Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi coder,

C#
Their are three table Reference1, Reference2 and Reference3

<pre lang="text">Reference1

123	pending
234	pending
345	pending
456	pending


Reference2 

345	going
456	going


Reference3

456	Done


Now I want out like

Reference1

123	pending
234	pending
345	going
456	Done


What I have tried:

I use below query but it will give me

456 Done

SELECT Reference3.*
FROM Reference1
JOIN Reference2
ON Reference2.ReferenceNumber = Reference1.ReferenceNumber
JOIN Reference3
ON Reference3.ReferenceNumber = Reference2.ReferenceNumber
Posted
Updated 11-Aug-16 20:18pm

1 solution

Hello ,
First I create three temporary table for explanation .
SQL
create table #tmp1
(
referenceno varchar(10),
statuscode varchar(20),
)
create table #tmp2
(
referenceno varchar(10),
statuscode varchar(20),
)
create table #tmp3
(
referenceno varchar(10),
statuscode varchar(20),
)


SQL
--Now insert data 
insert into #tmp1 values('123', 'pending')
insert into #tmp1 values('234', 'pending')
insert into #tmp1 values('345', 'pending')
insert into #tmp1 values('456', 'pending')
insert into #tmp1 values('567', 'pending')
insert into #tmp1 values('678', 'pending')


insert into #tmp2 values('345', 'going')
insert into #tmp2 values('456', 'going')
insert into #tmp2 values('567', 'going')


insert into #tmp3 values('456', 'done')
insert into #tmp3 values('567', 'done')
insert into #tmp3 values('678', 'done')


to get required OP , first make union all of those three tables .
and then use Row_Number with Partition by clause, to sequence them one by one by statuscode and lastly use Common Table Expression to get required OP .

SQL
;with CTE
as
(
select ROW_NUMBER() OVER (partition by referenceno  ORDER BY statuscode) AS RowNumber , *
from 
        (
select * from #tmp1 
union all
select * from #tmp2
union all
select * from #tmp3
)xx)

select referenceno , statuscode from cte  where RowNumber=1

OP
C#
referenceno	statuscode
123	pending
234	pending
345	going
456	done
567	done
678	done



Here another solution that may help you without using CTE
First Select only those records from #tmp1 or first table which are not present in other two tables (#tmp2 and #tmp3)
again select only those records from #tmp2 or second table which are not present in
last table (#tmp3)
and lastly make union all between those records sets .
SQL
select referenceno , statuscode from #tmp1 where referenceno
not in 
(
select referenceno from #tmp2
union all
select referenceno from #tmp3
)
union all

select referenceno , statuscode  from #tmp2 where referenceno
not in 
(
select referenceno from #tmp3
)
union all
select referenceno , statuscode  from #tmp3

Here also OP is
C#
referenceno	statuscode
123	pending
234	pending
345	going
456	done
567	done
678	done


Thanks
 
Share this answer
 
v3
Comments
amitesh1989 12-Aug-16 3:16am    
Thanks a Lot for you help can you tell how and where we use CTE
Animesh Datta 12-Aug-16 3:20am    
Glad to help you .
Please check below link
http://www.codeproject.com/Articles/265371/Common-Table-Expressions-CTE-in-SQL-SERVER
Karthik_Mahalingam 12-Aug-16 5:26am    
5 for your efforts
Animesh Datta 12-Aug-16 5:34am    
Thank You
amitesh1989 18-Aug-16 6:13am    
thanks Animesh

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



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