Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Recursion/

Author  Topic 

sneupane
Starting Member

28 Posts

Posted - 2011-10-18 : 09:58:12
I have a table like shown below:

Item Comp Quantity
A B 1
A C 2
B
C E 4
C F 6
D
E
F
G

If someone queries for item A
I would like to populate the result like shown below.

A B 1
A C 2
B
C E 4
C F 6
E
F

I tried using CTE, but no success. Any help would be appreciated!

Thanks
Sar



Sar

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-10-18 : 10:02:45
how does this line comes about ?

E G 4




KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-10-18 : 10:09:33
Recursive CTE

; with rcte as
(
select Item, Comp, Quantity
from tbl
where Item = 'A'

union all

select t.Item, t.Comp, t.Quantity
from rcte r
inner join tbl t on r.Comp = t.Item
)
select *
from rcte
order by Item



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

sneupane
Starting Member

28 Posts

Posted - 2011-11-08 : 19:28:49
I got whatch u mean.. got it thanks a lot..

Sar
Go to Top of Page
   

- Advertisement -