Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to SELECT from foreign table where criteria matches or return a null but only joining on one null.

Posted on 2015-02-17
2
Medium Priority
?
107 Views
Last Modified: 2015-02-17
Given
TableA
ID     Name
1         A
2         B
3         C
4         D
5         E

and

TableB
ID   CategoryID  
1        5
2        4
2        5
3        5
3        4
4        1
4        3
and joining the two tables above and applying some criteria somewhere in the selection of CategoryID = 5 I would like to get output column :

TableC
ID  Name  CategoryID  
1        A           5
2        B           5
3        C           5
4        D          null
5        E           null

Note that ID only returns once and basically we get the category id returned if there is a match but otherwise null.
I would prefer to avoid using grouping although you may say this is not possible.
Server is SQL server 2000
Thanks in advance!
0
Comment
Question by:dgloveruk
2 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 2000 total points
ID: 40613793
Select A.ID as ID, A.Name as Name, B.CategoryID as CategoryID
From TableA as A
left join (Select ID, CategoryID from TableB where CategoryID = 5) as B
on A.ID = B.ID
0
 

Author Closing Comment

by:dgloveruk
ID: 40614069
That's great , thanks very much!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

963 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