Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have a table with data :

C#
ID     Case_ID      Related_Case_ID
 1        11            22
 2        22            33
 3        05            47
 4        89            33
 5        33            44
 6        67            56


now I want to find all the related case of case_id=11 the result like :

C#
ID     Case_ID      Related_Case_ID
 1        11            22
 2        22            33
 5        33            44



Any suggestion really appreciated!
Posted

Refer this

http://technet.microsoft.com/en-us/library/ms186243%28v=sql.105%29.aspx[^]


Result :
SQL
create table ##table1 (id bigint identity(1,1),case_id bigint,Related_case_id bigint)

insert into ##table1(case_id ,Related_case_id) values(11,22)
insert into ##table1(case_id ,Related_case_id) values(22,33)
insert into ##table1(case_id ,Related_case_id) values(05,47)
insert into ##table1(case_id ,Related_case_id) values(89,33)
insert into ##table1(case_id ,Related_case_id) values(33,44)
insert into ##table1(case_id ,Related_case_id) values(67,56)


select *From ##table1

 with cte(id , pid)
as
(select case_id,Related_case_id  from ##table1 where case_id=11
union all
select a.pid ,t.Related_case_id from cte as a inner join ##table1 as  t on a.pid = t.case_id
)
select *from cte
 
Share this answer
 
v2
Comments
Mas11 19-May-14 3:40am    
THX A LOT IT WORK!
SQL
WITH    q AS 
        (
        SELECT  *
        FROM    Tbl1
        WHERE   Case_ID=11 
        UNION ALL
        SELECT  m.*
        FROM    Tbl1 m
        JOIN    q
        ON      m.Case_ID = q.Related_Case_ID
        )
SELECT  *
FROM    q

DEMO[^]
 
Share this answer
 
Comments
Mas11 19-May-14 3:40am    
thx a lot dear!

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