Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
102 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

721 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