Solved

SQL Left join, how to include empty rows

Posted on 2003-11-13
8
1,467 Views
Last Modified: 2012-06-27
I have got the following 2 tables:

amounts
--------
userfk
transfk
amount

AND

users
-------
userid
name

I would like to have a query, that returns for a certain transfk ALL users with either the amount or a NULL value. For example: I have got 3 users, for transfk = 14 only 2 users have got an amount:

userid  name    amount  transfk
------  -------     ------      --------
1       Ronald         3       14
2       John            4       14
3       Austin        NULL    14

What query do I have to perform. To make it a bit clearer, the tables contain the following values:

amounts:
userfk   transfk   amount
-------  --------  --------
1        14        3
2        14        4
2        17        1

users:
userid   name
-------  ----------
1        Ronald
2        John
3        Austin

Any suggestions?
0
Comment
Question by:dutchdevelopment
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 23

Expert Comment

by:adathelad
ID: 9738522
Hi,

This should dot it:

SELECT userid, name, amount
FROM users
     LEFT JOIN amounts ON userid = userfk
WHERE transfk IS NULL or transfk = 14

Cheers
0
 

Author Comment

by:dutchdevelopment
ID: 9738583
Unfortunately this won't work in all cases.

It'll work in this case because both users 1 and 2 have an entry for transfk = 14 and user 3 doesn't have an entry at all.

But when you do the same for transfk = 17 you will get no entry for user 1 (Ronald):

userid  name    amount  transfk
------  -------     ------      --------
2       John            1        17
3       Austin        NULL    17

Any clues?
0
 
LVL 12

Accepted Solution

by:
koutny earned 250 total points
ID: 9738624
SELECT userid, name, amount
FROM users
     LEFT JOIN amounts ON (userid = userfk AND transfk = 14)
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:dutchdevelopment
ID: 9738640
Thanks all.

I have used the answer of koutny as a starter with the following added:

SELECT userid, name, amount
FROM users
LEFT JOIN amounts ON (users.userid = amounts.userfk AND amounts.transfk = 17 OR amounts.transfk IS NULL)
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 9738836
This might do it (modify fields you need to see)

select * from users left join
(
SELECT userid, name, amount, transfk
FROM users
     LEFT JOIN amounts ON userid = userfk
WHERE transfk IS NULL or transfk = 14
) A on A.name = users.name

0
 
LVL 12

Expert Comment

by:koutny
ID: 9738907
Hi, I don't think that any addition or modificiation is needed.
It is the property of the LEFT OUTER JOIN that it will return ALL records from the table on the left side on the join (i.e. users) and it will fill in the remaining columns from the table on the right side of the join if the the ON (...) condition is true, otherwise it will fill those columns with null values.
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 9739489
You are right - delayed post on my part. I opened this thread before your response and obviously replied after you. We have such slow lines here that tomorrow and next week mean the same thing ;)

Your post is much better - simplier.
0
 

Author Comment

by:dutchdevelopment
ID: 9740918
koutny:
It is necessary to add the modification (at least in MS Access 2000).
I think that the addition of amounts.transfk = 17 has undone the "return ALL records"-effect of a LEFT OUTER JOIN.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
compre toata in where clue oracle 4 57
free version of MSSQL for over 10GB DB 19 59
Exchange 2007 standard - defrag (eseutul /d) 10 39
SYbase 4 27
In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question