Solved

How do I identify only the matched column value in SQL?

Posted on 2009-07-07
3
142 Views
Last Modified: 2012-05-07
Table1:
Number      Type                  
1                     1      
1                     1      
1                     2      
1                     2      
5                     3            
6                     4      
7                     5      
7                     5      
7                     5      

Table2:
Type             Code1   Code2   Code3   Code4            
1                       A           B          NULL    NULL      
2                       F          NULL    NULL        C  
3                       D            B         NULL     NULL
4                       D          NULL        F        NULL      
5                       A            E              X            B                  

SELECT * FROM Table1 t1, Table2 t2
WHERE t1.Type = t2.Type
AND  (t2.Code1 IN (A, B)
   OR t2.Code2 IN (A, B)
   OR t2.Code3 IN (A, B)
   OR t2.Code4 IN (A, B))

Result Set:
Number  Type     Code1   Code2   Code3   Code4  
1               1             A            B            NULL       NULL
5               3             D            B            NULL       NULL
7               5             A            E               X            B  

How do I display a result set which will only show the code that matched?

This is what I want to return...
Number    Type    Code
1                 1            A      
5                 3            B      
7                 5            A
0
Comment
Question by:seckel
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
pssandhu earned 500 total points
ID: 24795915
How about:
Select Number, Type, CASE WHEN CODE1 IN (A,B) Thne CODE1

                          WHEN CODE2 IN (A,B) Thne CODE2

                          WHEN CODE3 IN (A,B) Thne CODE3

                          WHEN CODE4 IN (A,B) Thne CODE4

                          ELSE 'N'

                     END as Code

FRom (

SELECT Number, t1.Type,  Code1, Code2, Code3, Code4

FROM Table1 t1, Table2 t2 

WHERE t1.Type = t2.Type 

AND  (t2.Code1 IN (A, B) 

   OR t2.Code2 IN (A, B)

   OR t2.Code3 IN (A, B)

   OR t2.Code4 IN (A, B))

) a 

Open in new window

0
 

Author Closing Comment

by:seckel
ID: 31600679
It worked!  Thank you very much.
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24796220
No Problem.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

920 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

15 Experts available now in Live!

Get 1:1 Help Now