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 2000 Forums
 SQL Server Development (2000)
 query join help

Author  Topic 

thanksfor help
Posting Yak Master

106 Posts

Posted - 2006-09-22 : 12:55:26
Hi,

I have a table in following structure

item_no level item_name
1234 1 ABCD
1234 2 xyz
1234 3 pqrs
8907 3 jack
8907 2 jim
8907 1 paul
2378 2 julie
2378 1 nancy
2378 3 emily

Select on the above table shld return

item_no l1 l1_name l2 l2_name l3 l3_name
1234 1 ABCD 2 XYZ 3 pqrs
8907 1 Paul 2 jim 3 jack
2378 1 nancy 2 julie 3 emily

Any help is very much appreciated.

X002548
Not Just a Number

15586 Posts

Posted - 2006-09-22 : 13:01:04
Read this

http://weblogs.sqlteam.com/brettk/archive/2005/02/23/4171.aspx

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-09-22 : 13:04:25
select item_no, 1, t1.item_name, 2, t2.item_name, 3, t3.item_name, 4, t4.item_name
from (select distinct item_no from tbl) t
join tbl t1
on t.item_no = t1.item_no
and t1.level = 1
left join tbl t2
on t.item_no = t2.item_no
and t2.level = 2
left join tbl t3
on t.item_no = t3.item_no
and t3.level = 3
left join tbl t4
on t.item_no = t4.item_no
and t4.level = 4


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

thanksfor help
Posting Yak Master

106 Posts

Posted - 2006-09-22 : 14:48:50
Thanks it worked
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-09-23 : 12:29:15
Also refer http://sqljunkies.com/WebLog/amachanic/archive/2004/11/10/5065.aspx?Pending=true

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -