I need to be able to show results for a row that doesn't actually exist in a second table. I have tried left join, but it doesn't seem to work for what I need to do. I have 2 tables, for example:
Table1
NAME | KEY
John 12345
Frank 23456
Table2
KEY | LIST | STATUS
12345 10001 1
12345 10003 0
23456 10001 1
23456 10002 1
I need to be able to show results like this :
NAME | KEY | LIST | STATUS
John 12345 10001 1
John 12345 10002 (null)
John 12345 10003 0
Frank 23456 10001 1
Frank 23456 10002 1
Frank 23456 10003 (null)
But I can't figure out how to do this because the records that would return a null value don't actually exist in the second table. If I run them one at a time, I can kind of get the results I need by doing a UNION ALL, but this is not efficient (I also had to re-order my results, which is making it harder as well)
SELECT b.list, a.name, a.key, b.status
FROM table1 a JOIN table2 b ON a.key = b.key
WHERE a.name = 'John'
UNION ALL
SELECT distinct(b.list), NULL AS "a.name", NULL AS "a.key", NULL AS "b.status"
FROM table2 b
This isn't an ideal solution either because I'm pulling the rest of the lists, but the other fields end up being null, so I have to paste them in manually when I move my results to excel.
Any ideas? I really thought I should be able to do this with a left join, but nothing I'm doing seems to work.
Use a cross join to get all the rows. Then use left join to bring in the existing values:
select t1.key, l.list, t2.status
from table1 t1 cross join
(select distinct list from table2) l left join
table2 t2
on t1.key = t2.key and t1.list = t2.list;
Try FULL OUTER JOIN
Example:
SELECT A.NAME, A.KEY, A.LIST, B.STATUS
FROM Table1 A
FULL OUTER JOIN Table2 B
ON A.KEY = B.KEY AND A.LIST = B.LIST
If don't want rows of the second table that they have not a reference in the first table add the condition:
WHERE A.KEY IS NOT NULL
Here is what I came up with. The first two queries are used to get all possible combinations of NAME, KEY and LIST together. The final query joins back to Table2 to get STATUS, returning NULL where STATUS does not exist.
;WITH Lists
AS
(
SELECT DISTINCT LIST
FROM Table2
),
KeyList AS
(
SELECT [NAME], [KEY], [LIST]
FROM Lists CROSS JOIN Table1
)
SELECT a.[NAME], a.[KEY],A.[LIST],b.[STATUS]
FROM KeyList a
LEFT JOIN Table2 b
ON a.[KEY] = b.[KEY]
AND a.[LIST] = b.[LIST]
Related
I have two tables, Table1 and Table2 which have in common two fields EmployeeId and ProjectId
Table1
EmployeeId
ProjectId
1
111
2
222
3
333
4
444
Table2
ProjectId
EmployeeId
222
5
111
1
444
4
333
2
555
8
I want to write a query that creates a column called 'Match' indicating whether an EmployeeId/ProjectId combo that appears in Table1 also appears in Table2. For example the first and fourth rows of Table1 should be indicated as matches since those combos appear in Table2 The final output should look like the following :
EmployeeId
ProjectId
Match
1
111
yes
2
222
no
3
333
no
4
444
yes
If anyone knows how to write this sort of query, I would really appreciate the help.
You need a left join on the field of employeeId and ProjectId.
So you will use table1 as the left table, and join table2 as the right table. Then where table2 fails to join, you will get NULL and you can write NO for the match...
SELECT
t1.employeeId,
t1.projectId,
CASE
WHEN t2.employeeId IS NULL THEN 'no' ELSE 'yes'
END AS match
FROM
table1 AS t1
LEFT JOIN
table2 AS t2
ON t2.employeeId = t1.employeeId
AND t2.projectId = t2.projectId
The left table, table1, will serve as the 'permanent' data that populates in the results-set, ie. you will get the whole table (t1) in the output.
The right table, table2, will only join, when there is a condition that employeeId matches AND projectId matches.
Because we choose a left join, the records from t1 that don't have a match in t2 will result in NULL from t2.
I have two tables
Table 1:
color_id | label
---------|------
2 | 0
3 | 0
2 | 0
1 | 0
4 | 1
4 | 1
5 | 0
Table 2:
color_id
--------
2
1
4
I want a query that just gives me results for color_ids that are present in Table 2
So, I wrote:
SELECT *
FROM table1
LEFT JOIN table2
ON table1.color_id = table2.color_id
WHERE table2.color_id IS NOT NULL
however, the above gives duplicates as well. Meaning I get
2 | 0
2 | 0
1 | 0
4 | 1
4 | 1
I don't want the duplicates in the results. I just want unique items.
I want a query that just gives me results for color_ids that are present in Table 2
So, you shouldn't use LEFT JOIN in this case:
SELECT DISTINCT a.color_id, a.label
FROM table_1 a JOIN table_2 b
ON a.color_id = b.color_id
When you add the keyword Left (or Right or full) to a join specifier, you make the join an outer join. This means that you get all the rows from one side of the join, and only those rows from the other side that match. If you only want the rows from table_1 where the color_id is in table_2, then you want an inner join, specified by writing inner join or just writing join, without a left, right or full.
to eliminate duplicates, add the keyword distinct to the select clause...
Select distinct color_id, label
From table1 t1
join table2 t2
on t2.color_id = t1.color_id
Try the below query
SELECT DISTINCT color_id
FROM table_1 T1
WHERE EXISTS (SELECT 1 FROM table_2 T2 where T1.color_id = T2.color_id)
Use an inner join and a distinct clause:
SELECT DISTINCT table1.color_id, table1.label
FROM table1
INNER JOIN table2
ON table1.color_id = table2.color_id
What you are looking for is an INNER JOIN combined with a
SELECT distinct table1.color_id, tabl1.label
FROM table1
INNER JOIN table2 ON table1.color_id = table2.color_id
This eliminates any item in table1 not present in table 2 and duplicated rows.
the reason of that is you used Left Join, which will keep all obs in table1.
Try this:
SELECT table1.* FROM table1 Inner JOIN table2 ON table1.color_id = table2.color_id
this should works as actually all table2 obs are in table1. To be more serious, if table2 has obs that are not in table1 and you do want to keep them, replace inner join with right join.
in my database i have 2 tables.
table1
i have ID and NAMES
table2
i have ID, IDASSOCIATION, QUANTITY
so
i have 2 names in table1:
john and tom
and in table2 i have 3 lignes
john, 1
tom, 1
john, 1
nombre one is the quantity
in my result i want get
john = 2
and tom = 1
so i do this:
sql = "SELECT t1.*, t2.IDASSOCIATION, (SELECT SUM(t2.id_qte) FROM associationdepotarticle t2 WHERE t1.fusiontable = t2.fusiontable GROUP BY t2.IDASSOCIATION) as id_qte FROM articletable t1, associationdepotarticle t2";
but i not get this:
john = 2
tom = 1
why ? what i will do, i need correction please
You can just join the tables together and use sum:
select t1.name, sum(t2.quantity)
from table1 t1
join table2 t2 on t1.id = t2.idassociation
group by t1.name
It's not completely clear from your sample data what to join on, but I assume it's the idassociation field. If you want to return those names in table1 which aren't in table2, then use an outer join.
I am attempting to do something using MSSQL that I believe is possible (easily) but I do not know how to vocalize the correct search string. I have the situation below.
Table A
UID | Value....
1 | a
2 | b
3 | c
Table B
PartTypes_uid_fk | Value....
1 | a
1 | b
1 | c
1 | d
1 | e
3 | 67
3 | 1354
I am attempting to get the following result, query Table A for all results {TableA.*} and on the same row result show the number of table b references {count TableB.tableA_fk}
What I have so far is the following.
SELECT DISTINCT t1.uid, CONVERT(varchar(MAX), t1.Name) AS Name, CONVERT(varchar(MAX), t1.Description) AS Description,
Count(t2.Items_uid_fk) OVER (Partition By t2.PartTypes_uid_fk) as Count
FROM [Table1] as t1 left outer join Table2 as t2 on t2.PartTypes_uid_fk=t1.uid;
This works for all of Table A records with an associated record in Table B but if there are 0 entries in Table B it won't work. The conversion of the varchars was required due to the fact they are ntext format and it was distinct.
Thank you for all your help in advance.
Stephen
Instead of running into problems with the GROUP BY on N/TEXT columns, and to run faster, you would want to pre-aggregate the B table and LEFT JOIN that against A.
select t1.*, ISNULL(t2.c, 0) AS CountOfB
from table1 t1
left join
(
select parttypes_uid_fk, count(*) c
from table2
group by parttypes_uid_fk
) t2 on t2.PartTypes_uid_fk=t1.uid
It's easier than that:
SELECT t1.uid, t1.Name, COUNT(*)
FROM [Table1] t1
LEFT JOIN [Table2] t2 ON t2.PartTypes_uid_fk = t1.uid
GROUP BY t1.uid, t1.Name
Your query should be
SELECT t1.uid,
CONVERT(varchar(MAX), t1.Name) AS Name,
CONVERT(varchar(MAX), t1.Description) AS Description,
Count(t2.Items_uid_fk) CountItems
FROM [Table1] as t1 left outer join Table2 as t2 on t1.uid = t2.PartTypes_uid_fk
GROUP BY uid, t1.name, t1.Description;
I could really use some help with the following SQL Select statement scenario:
I need to select all rows from a table conditionally depending on whether a userID has already entered data into a second table with the same ID.
Example:
Select all rows from TABLE A for idNumber where idNumber not in
TABLE B
but for each idNumber that IS in TABLE B, still return row unless a
specific userID is in that row in TABLE B.
TABLE A
========
idNumber|type|Date
1 A 01/01/01
2 A 01/01/01
3 B 01/01/01
4 B 01/01/01
5 B 01/01/01
TABLE B
========
idNumber|type|userID
1 A 0000
3 B 0000
4 B 1111
userID to exclude records for = 1111
SQL Query should return:
idNumber|type|Date
1 A 01/01/01
2 A 01/01/01
3 B 01/01/01
5 B 01/01/01
Apologies for the long winded post but i hope it makes sense.
Many thanks in advance,
ukjezza.!!
Select idNumber, type, Date
From TableA
Where Not Exists (
Select 1
From TableB
Where TableB.idNumber = TableA.idNumber
And TableB.userID = 1111
)
Another choice:
Select TableA.idNumber, TableA.type, TableA.Date
From TableA
Left Join TableB
On TableB.idNumber = TableA.idNumber
And TableB.userId = 1111
Where TableB.idNumber Is Null
Looks like a LEFT JOIN and COALESCE could take care of it:
SELECT a.*
FROM TableA as a
LEFT JOIN TableB as b
ON a.idNumber = b.idNumber
WHERE COALESCE(b.userID, -1) != 1111
select A.*
from TableA as A
left outer join TableB as B
on A.idNumber = B.idNumber
where B.idNumber is null or
B.userID <> '1111'