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
82 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 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now