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
99 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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

636 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