I have 2 tables each with same fields basically containing
table1.ItemCode table1.Qty
table2.ItemCode table2.Qty
i am querying these two tables from sql by the following command
SELECT c.Code ,
t1.Code ,
t1.Qty ,
t2.Code ,
t2.Qty
FROM ( SELECT Code
FROM dbo.Table1
UNION
SELECT Code
FROM dbo.Table2
) c
LEFT OUTER JOIN dbo.Table1 t1 ON c.Code = t1.Code
LEFT OUTER JOIN dbo.Table2 t2 ON c.Code = t2.Code
WHERE t1.Code IS NULL
OR t2.Code IS NULL
OR t1.Qty <> t2.Qty
this query provides me with the item codes that exist in both tables
that have only different quantities
for example if item: x has qty 2 and in the second table Item x has qty 4
this item would show as: x 2 4
however if Item x has qty 2 and in the second table also the same qty
this item will not appear in the result
the problem is that in my situation these 2 tables are two data Tables in my asp.net
project
i need to execute the same query but on these two data tables
how can that be done or is their any other possible solution to get my result from these 2
data tables
This is quite straightforward.
DataTable table1;
DataTable table2;
//Initialize your DataTables here
var result = (from row1 in table1.AsEnumerable()
join row2 in dataTable2.AsEnumerable() on row1["Code"] equals row2["Code"]
where !object.Equals(row1["Qty"], row2["Qty"])
select new { Code = row1["Code"], table1Qty = row1["Qty"], table2Qty = row2["Qty"] })
.ToArray();
Rows from the two tables are joined on Code.
join row2 in dataTable2.AsEnumerable() on row1["Code"]
Subsequently, rows with the same Qty values are filtered
where !object.Equals(row1["Qty"], row2["Qty"])
Related
I have a table of ratings
|EMPLOYEE|Rating|
1 B
2 B
3 C
4 NULL
5 NULL
6 NULL
and i want to retrieve the count of the grades by each grading like so
Result set
|Rating|Count|
A 0
B 2
C 1
D 0
E 0
I used this query but the grades that isnt in the table will jsut appear as null
select rating,count(rating) from table group by rating
I also used this query which is basically a pivot of the above result set but for some reason it shows 3 rows of repeating data instead of just 1
select (select count(rating) from table where rating = 'E'),(select count(rating) from table where rating = 'D'),(select count(rating) from table where rating = 'C'),(select count(rating) from table where rating = 'B'),(select count(rating) from table where rating = 'A') from table group by rating
If you had a table for the assignable ratings then it would be quite simple (and flexible)
e.g. consider :-
Your existing table.
DROP TABLE IF EXISTS mytable;
CREATE TABLE IF NOT EXISTS mytable (employee TEXT, rating text);
INSERT INTO mytable VALUES (1,'B'),(2,'B'),(3,'C'),(4,null),(5,null),(6,null);
The rating table.
DROP TABLE IF EXISTS rating;
CREATE TABLE IF NOT EXISTS rating (rating);
INSERT INTO rating VALUES('A'),('B'),('C'),('D'),('E');
Then :-
SELECT rating.rating, (SELECT count(*) FROM mytable WHERE rating.rating = mytable.rating) FROM rating;
Results in :-
Flexibility
Add some new ratings e.g. as per :-
INSERT INTO rating VALUES('X'),('Y'),('Z');
And then run:--
SELECT rating.rating, (SELECT count(*) FROM mytable WHERE rating.rating = mytable.rating) FROM rating;
results in :-
i have 3 tables
table 1( one id to one name)
ID|name
1| john
2|mike
3|olga
4|juliet
table 2 ()
ID|adress
1|xxx
1|yyy
2|xx
2|z
3|xxx
3|yy
table 3 ()
address|buildings
xxx|flat
xxx|building
z|flat
z|building
z|park
z|lot
yy|building
yy|park
i wish to find the name of those wanting to stay at both building and flat, all values are random so i can only search with buildings, i have already managed to query for only one type of buildings but i am missing something as to validate the needed AND
You have to join all 3 tables and group by name:
SELECT t1.name FROM table1 t1
INNER JOIN table2 t2 ON t2.id = t1.id
INNER JOIN table3 t3 ON t3.address = t2.address
WHERE t3.buildings IN ('building', 'flat')
GROUP BY t1.name
HAVING COUNT(DISTINCT t3.buildings) = 2
I have a gridview and originally I was binding data from one table (Table 1) and it was straightforward
gvUsers.dataSource = class.getUsers()
gvUsers.databind()
Function getUsers () As List (Of Users)
Return (From X in Table1 Select x).ToList()
End Function
However, some of my data are pointing to another table and there is no relationship between tables ( no foreign key)
Table 1
UID Name Age Blood Type Test Type
1 Sam 22 2 3
2 Jane 23 2 4
Table 2
ID Domain Code Description
1 Blood Type A A
2 Blood Type B B
3 Test Type 1 1
4 Test Type 2 2
Currently In the gridView I see Blood Type as values 2 and Test type 3, 4 ( The second table ID) but I should get the Code column values in Table 2.
How can I join those two tables - I know if there is foreign key but the fact a column name is equivelant to row data name makes it hard for me to figure out!
Cheers
Try Code
var result = (from p in Table1.AsEnumerable()
join Blood in Table2.AsEnumerable() on p.BloodType equals Blood .ID
join Test in Table2.AsEnumerable() on p.TestTyoe equals Test .ID
select new With
{
uid = p.UID,
Name = p.name,
Age = p.age,
BloodType = Blood.Code,
TestType = Test .Code
}).ToList();
Sql Query IS :
select UID ,Name,Age,B.Code as BloodType ,T.Code as TestType From Table1
inner join Table2 B on Table1.BloodType = B.ID
inner join Table2 T on Table1.TestType= T.ID
The Used Vb then
Dim result = from p in Table1.AsEnumerable()
join Blood in Table2.AsEnumerable() on p.BloodType equals Blood .ID
join Test in Table2.AsEnumerable() on p.TestTyoe equals Test .ID
select
p.UID,
p.name,
p.age,
Blood.Code,
Test.Code
gvUsers.DataSource = result
Try this one:
select table1.*, table2.* from table1
inner join table2 on table1.Blood Type = table2.id
You can select you desired columns from both tables.
I'm trying to join 3 tables in a view; here is the situation:
I have 3 tables....
first one is [ADM_Panels] that contain information of panels with
columns:panel_id, panel_number, YearID
second one is [ADM_PanelsDtl] with
columns: [panel_id], [pnlmember_id]
I have another table called "GEN_Years" with 2 columns
table 3: [GEN_Years]
columns:[YearID] ,[Year]
I tried some thing like this but my select statement should return 2 different tables
SELECT YearID,count(pnlmember_id) Reviewer FROM [ADM_PanelsDtl] join [ADM_Panels]
on [ADM_PanelsDtl].[panel_id]=[ADM_Panels].[panel_id]group by YearID
select YearID,count([panel_id])Panel from dbo.ADM_Panels group by YearID
But i want to show my result in a table with fields
[YearID], [Year], count(pnlmember_id), count([panel_id])Panel
by joining the above 3 tables. Any solution on how to do it?
I don't want to use temporary table, I just want want to join these tables
May be lyk this
SELECT A.[YearID], c.[Year], a.Reviewer, B.Panel
FROM (SELECT YearID,
Count(pnlmember_id) Reviewer
FROM [ADM_PanelsDtl]
JOIN [ADM_Panels]
ON [ADM_PanelsDtl].[panel_id] = [ADM_Panels].[panel_id]
GROUP BY YearID) A
JOIN (SELECT YearID,
Count([panel_id])Panel
FROM dbo.ADM_Panels
GROUP BY YearID) B
ON a.YearID = b.YearID join GEN_Years c on a.YearID = c.YearID
I have two tables, one with objects, one with properties of the objects. Both tables have a personal ID and a date as "key", but since multiple orders of objects can be done by one person on a single day, it doesn't match well. I do know however, that the entries are entered in the same order in both tables, so it is possible to join on the order, if the personID and date are the same.
This is what I want to accomplish:
Table 1:
PersonID Date Object
1 20-08-2013 A
2 13-11-2013 B
2 13-11-2013 C
2 13-11-2013 D
3 21-11-2013 E
Table 2:
PersonID Date Property
4 05-05-2013 $
1 20-08-2013 ^
2 13-11-2013 /
2 13-11-2013 *
2 13-11-2013 +
3 21-11-2013 &
Result:
PersonID Date Object Property
4 05-05-2013 $
1 20-08-2013 A ^
2 13-11-2013 B /
2 13-11-2013 C *
2 13-11-2013 D +
3 21-11-2013 E &
So what I want to do, is join the two tables and "zip" the group of entries that have the same (PersonID,Date) "key".
Something called "Slick" seems to have this (see here), but I'd like to do it in SQLite.
Any advice would be amazing!
You are on the right track. Why not just do a LEFT JOIN between the tables like
select t2.PersonID,
t2.Date,
t1.Object,
t2.Property
from table2 t2
left join table1 t1 on t2.PersonID = t1.PersonID
order by t2.PersonID
Use a additional column to make every key unique in both tables. For example in SQLite you could use RowIDs to keep track of the order of insertion. To store this additional column in the database itself might be useful for other queries as well, but you do not have to store this.
First add the column ID to both tables, the DDL queries should now look like this: (make sure you do not add the primary key constraint until both tables are filled.
CREATE TABLE table1 (
ID,
PersonID,
Date,
Object
);
CREATE TABLE table2 (
ID,
PersonID,
Date,
Property
);
Now populate the ID column. You can adjust the ID to your liking. Make sure you do this for table2 as well:
UPDATE table1
SET ID =(
SELECT table1.PersonID || '-' || table1.Date || '-' || count( * )
FROM table1 tB
WHERE table1.RowID >= tB.RowID
AND
table1.PersonID == tB.PersonID
AND
table1.Date == tB.Date
);
Now you can join them:
SELECT t2.PersonID,
t2.Date,
t1.Object,
t2.Property
FROM table2 t2
LEFT JOIN table1 t1
ON t2.ID = t1.ID;