question

ajean42 avatar image
ajean42 asked

Little Joining Problems

Hi people! So I have a little logic problem that I might just be too tired to think of a solution. So I have 2 tables. Cards card_id, AccessLevel, AccessLevelTmp AccessLevel accessLevel_Id, AccessLevel_Desc I must link the table Cards with AccessLevel and show the description of both AccessLevel and AccessLevelTmp. My problem is, both AccessLevel and AccessLevelTmp can be null in the Cards table and I don't want those lines to come out. So, right now, my select looks like this. but it does return lines where both are null. If I put inner joins, some lines that I want will be skipped. SELECT card.card_id, acle.AccessLevel_Desc, acle2.AccessLevel_Desc FROM Cards card LEFT OUTER JOIN AccessLevel acle ON acle.AccessLevel_Id = card.AccessLevel LEFT OUTER JOIN AccessLevel acle2 ON acle2.AccessLevel_Id = card.AccessLevelTmp So, either only AccessLevel is null either only AccessLevelTmp is null, either both are null. I don't want the lines where both are null, but I want every others. Don't tell me to change how the data is, it is impossible. The hardware need those lines, but I don't want them on the reports. I know there could be plenty of others ways to do it, but I really wonder if it is possible to do it in a SINGLE SELECT STATEMENT. It is the only solution that interests me. Thank you for taking time to read this!
sql-server-2008-r2queryjoins
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Kev Riley avatar image
Kev Riley answered
Your join is fine, simply add a where clause where ( acle.AccessLevel_Desc is not null or acle2.AccessLevel_Desc is not null )
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ajean42 avatar image ajean42 commented ·
omg, that was so obvious haha Thanks for the input!
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.