Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi , I have a table items with itemid,itemname,qty,unit, pitemidNow I am able to get the value of itemname and parent item name withselect c.iname as cname,p.iname as pnamefrom item pjoin item c on p.itemid=c.pitemidnow i want get the in the form Parentitem1Childitem11Childitem12Parentitem2Childitem21Childitem22
khtan
In (Som, Ni, Yak)
17689 Posts
Posted - 2006-08-22 : 01:35:22
This looks more like a presentation issue. Just use order by to return the result in the order you want and display it in your front end applicationKH
vmurali
Yak Posting Veteran
88 Posts
Posted - 2006-08-22 : 02:14:51
I want to get this using a SQL Query using self join
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2006-08-22 : 02:35:40
This should do the trick
select i1.inamefrom item i1left join item i2 on i2.itemid = i1.pitemidorder by isnull(i2.iname, i1.iname), i2.iname
Peter LarssonHelsingborg, Sweden
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2006-08-22 : 02:42:23
Oh, here is the test data
insert @itemselect 0 itemid, 'Parentitem1' iname, null pitemid union allselect 1, 'Parentitem2', null union allselect 2, 'Childitem12', 0 union allselect 3, 'Childitem21', 1 union allselect 4, 'Childitem11', 0 union allselect 5, 'Childitem22', 1