Click here to Skip to main content
15,891,721 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
TEMP1
--------------------
COL1 COL2 COL3
--------------------
MW12 null A
MW13 13 A
null null A
null null A
MW16 16 A
MW17 null A
MW18 18 A
MW20 20 A
MW22 22 A

TEMP2
--------------------
COL1 COL2 COL3
--------------------
null null A
null null A
null 14 A
MW15 15 A
MW16 16 A
MW17 17 A
MW19 18 A
MW20 21 A
null 23 A

Expected RESULTS:

MW12
MW13
14
NULL
MW16
MW17
MW18
MW20
MW22,23(RECORDS SEPARATE)

What I have tried:

SELECT DISTINCT
(
CASE
WHEN T2.COL1 IS NULL AND T2.COL2 IS NULL AND T1.COL2 IS NULL THEN T1.COL1--1
WHEN T2.COL1 IS NULL AND T2.COL2 IS NULL THEN T1.COL1--2
WHEN T1.COL2 IS NULL AND T2.COL1 IS NULL AND T1.COL1 IS NULL THEN T2.COL2--3
WHEN T1.COL2=T2.COL2 AND T2.COL1=T1.COL1 THEN T1.COL1--5
WHEN T2.COL1=T1.COL1 AND T1.COL2 IS NULL THEN T1.COL1--6
WHEN T1.COL2=T2.COL2 AND T2.COL1!=T1.COL1 THEN T1.COL1--7
WHEN T1.COL2!=T2.COL2 AND T2.COL1=T1.COL1 THEN T1.COL1--8
WHEN T1.COL2!=T2.COL2 AND T2.COL1 IS NULL THEN T1.COL2 --9
WHEN T1.COL2 IS NULL AND T1.COL1 IS NULL THEN NULL--4
END
) EMPLOYEE_ID
FROM TEMP1 T1,TEMP2 T2 WHERE T1.COL3=T2.COL3;
Posted
Updated 1-Nov-17 2:58am

SELECT  DISTINCT
           CASE WHEN  (t1.col1 IS NULL) AND (t2.col1 IS NOT NULL)  THEN null 
                WHEN  t2.col1 IS NULL THEN cast(t2.col2 as Varchar(50)) 
                 ELSE t1.col1  END AS Result
 FROM #TEMP1 T1 INNER JOIN #TEMP2 T2
                           ON(T1.COL3=T2.COL3)
 
Share this answer
 
Comments
CHill60 1-Nov-17 9:14am    
5'd as this does bring back exactly as the OP wanted
Santosh kumar Pithani 1-Nov-17 23:53pm    
Thank you CHill60!
It is not at all clear what you are actually trying to achieve!

Firstly, I think you need to learn about the COALESCE[^] function

As you seem to need values from both tables then you also need to know about the UNION[^] operator

There doesn't seem to be any logic as to why you are not also wanting MW15 and MW19 in your results, but the closest I can get is with
SQL
select COALESCE(col1, col2) from temp1 
UNION 
SELECT COALESCE(col1, col2)  FROM temp2
Note that I've used UNION and not UNION ALL to effectively remove the duplicates for me.

Finally, the fact that your code ran at all implies that you have stored numbers as character type fields - that is very poor table design.
 
Share this answer
 
select distinct isnull(isnull(isnull(t1.COL1,t1.COL2),t2.COL1),t2.col2)

FROM TEMP1 T1,TEMP2 T2 WHERE T1.COL3=T2.COL3 
 
Share this answer
 
Comments
CHill60 1-Nov-17 9:05am    
It wasn't me that down-voted your solution, but here are some possible reasons why that happened...
1. Instead of using isnull(isnull(isnull(... consider using COALESCE - it is far more efficient that nesting calls to isnull

2. The way you are joining the tables is very old fashioned - it is better practice to use an explicit JOIN

3. Your solution returns rows the OP did not want (MW15 and MW19). To be fair, so does mine, but at least I pointed it out

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900