| Author |
Topic |
|
cruxmagi
Starting Member
38 Posts |
Posted - 2009-02-19 : 04:05:06
|
| I have a table which has the following structureCmtId ParentId Level Cmt359 358 1 MTB360 NULL 0 no gravity361 360 1 no gravity362 NULL 0 wethepeople beyond363 NULL 0 wethepeople beyond364 363 1 wethepeople beyond365 NULL 0 wethepeople beyond366 364 2 wethepeople beyond367 366 3 wethepeople beyond i want the result as like as followsCmtId ParentId Level Cmt359 358 1 MTB360 NULL 0 no gravity361 360 1 no gravity362 NULL 0 wethepeople beyond363 NULL 0 wethepeople beyond364 363 1 wethepeople beyond366 364 2 wethepeople beyond367 366 3 wethepeople beyond365 NULL 0 wethepeople beyondcould you help me out |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-19 : 04:20:24
|
| select * from @tab order by case when cmt='wethepeople beyond' then Level end |
 |
|
|
cruxmagi
Starting Member
38 Posts |
Posted - 2009-02-19 : 04:24:41
|
Sorrycmt='wethepeople beyond' is a dummy text,it might not be repeated,to give a structure i just filled it.quote: Originally posted by bklr select * from @tab order by case when cmt='wethepeople beyond' then Level end
|
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-02-19 : 05:04:47
|
| Can you explain what output you need ? |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-19 : 05:12:48
|
Try this Once,declare @temp table (CmtId INT, ParentId INT,Level varchar(32),Cmt VARCHAR(32))insert into @tempselect 359, 358, 1, 'MTB' UNION allselect 360 ,NULL, 0, 'no gravity' UNION allselect 361, 360, 1, 'no gravity' UNION allselect 362,NULL, 0, 'wethepeople beyond' UNION allselect 363, NULL, 0, 'wethepeople beyond' UNION allselect 364, 363, 1 ,'wethepeople beyond' UNION allselect 365, NULL, 0, 'wethepeople beyond' UNION allselect 366, 364 ,2, 'wethepeople beyond' UNION allselect 367, 366 ,3, 'wethepeople beyond';WITH cte (CmtId, ParentId, Level, Cmt, path,Indent)AS ( SELECT CmtId, ParentID, level, Cmt, CONVERT(VARCHAR, ROW_NUMBER() OVER (ORDER BY cmt)), 0 FROM @temp WHERE ParentID IS NULL UNION ALL SELECT s.CmtId, s.ParentID, s.level, s.Cmt, CONVERT(VARCHAR, y.Path + ',' + CONVERT(VARCHAR, ROW_NUMBER() OVER (ORDER BY s.cmt DESC))), y.Indent + 1 FROM @temp AS s INNER JOIN cte AS y ON y.CmtId = s.ParentID)SELECT CmtId, ParentID, REPLICATE('', Indent) + cmt, levelFROM cteORDER BY Path |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-19 : 06:12:56
|
| declare @temp table (CmtId INT, ParentId INT,Level varchar(32),Cmt VARCHAR(32))insert into @tempselect 359, 358, 1, 'MTB' UNION allselect 360 ,NULL, 0, 'no gravity' UNION allselect 361, 360, 1, 'no gravity' UNION allselect 362,NULL, 0, 'wethepeople beyond' UNION allselect 363, NULL, 0, 'wethepeople beyond' UNION allselect 364, 363, 1 ,'wethepeople beyond' UNION allselect 365, NULL, 0, 'wethepeople beyond' UNION allselect 366, 364 ,2, 'wethepeople beyond' UNION allselect 367, 366 ,3, 'wethepeople beyond';with cte ( cmtid,parentid,level,cmt,path ) as( select *,convert(varchar(max),cmtid) as path from @temp where parentid is null union all select t.cmtid,t.parentid,t.level,t.cmt,c.path+'-'+ convert(varchar(32),t.cmtid) from @temp t inner join cte c on c.cmtid = t.parentid )select c1.cmtid,c1.parentid,c1.level,c1.cmt from @temp c1 left join cte t1 on t1.cmtid = c1.cmtidorder by path |
 |
|
|
|
|
|