Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 exclude null values?

Author  Topic 

allan8964
Posting Yak Master

249 Posts

Posted - 2013-09-15 : 18:22:15
Hi there,

I got table1 and table2 and there is a relationship in it like this:
table1 (FK set for UserID)
UserID | Type | Code ...

table2 (PK set for UserId)
UserId | UserName ...


For some reason some of the IDs in table1 are null and table2 is the table to hold all thes userIDs:

Now the problem is when I use
Select UserID, ...
from table1 t1
inner join table2 t2 on t1.UserId = t2.UserId
group by UserId


then all the null rows in table1 will be filtered. How can I include them in the joins and make them show as something 'no values'?
Thanks in advance.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2013-09-15 : 23:57:01
[code]Select t1.UserID, ...
from table1 t1
LEFT join table2 t2 on t1.UserId = t2.UserId
group by t1.UserId[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

mohan123
Constraint Violating Yak Guru

252 Posts

Posted - 2013-09-16 : 03:44:19
Select ( CASE WHEN t1.UserID IS NULL THEN 'NoValue' ELSE t1.UserID END) AS Value
from table1 t1
LEFT join table2 t2 on t1.UserId = t2.UserId
group by t1.UserId

P.V.P.MOhan
Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 2013-09-16 : 12:40:46
Cool, Left Join, I forgot that. Thanks lot for you both.
Go to Top of Page
   

- Advertisement -