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.
| Author |
Topic |
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2009-10-17 : 01:54:21
|
| I have category table.. as belowcatid---- catname parentcatid1 ------ Cat1 ------ 02 ------ Cat2 ------ 03 ------ Cat3 ------ 04 ------ Cat1_sub1 -- 15 ------ Cat1_sub2 -- 16 ------ Cat1sub_sub - 47 ------ Cat2_sub1 --- 28 ------ Cat3_sub1 --- 39 ------ Cat2_subsub -- 7this is simple category table with its category id and parent category id upto NTH LEVEL.I want LEVEL column With Same order by for Each category and its sub categories... as below.catid catname parentcatid --- levels1 --- Cat1 0 --- 04 --- Cat1_sub1 1 --- 16 --- Cat1sub_sub 4 --- 210 --- Cat1subsub_sub 6 --- 35 --- Cat1_sub2 1 --- 12 --- Cat2 0 --- 07 --- Cat2_sub1 2 --- 19 --- Cat2_subsub 7 --- 23 --- Cat3 0 --- 0 8 --- Cat3_sub1 3 --- 1 In above output levels column with ordered output which shows first category and its sub level categories... |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-10-17 : 02:24:16
|
[code]DECLARE @sample TABLE( catid int, catname varchar(15), parentcatid int)INSERT INTO @sampleSELECT 1 , 'Cat1' , 0 UNION ALLSELECT 2 , 'Cat2' , 0 UNION ALLSELECT 3 , 'Cat3' , 0 UNION ALLSELECT 4 , 'Cat1_sub1' , 1 UNION ALLSELECT 5 , 'Cat1_sub2' , 1 UNION ALLSELECT 6 , 'Cat1sub_sub' , 4 UNION ALLSELECT 7 , 'Cat2_sub1' , 2 UNION ALLSELECT 8 , 'Cat3_sub1' , 3 UNION ALLSELECT 9 , 'Cat2_subsub' , 7 UNION ALLSELECT 10 , 'Cat2_subsub' , 6; WITH cteAS( SELECT catid, catname, parentcatid, levels = 0 FROM @sample WHERE parentcatid = 0 UNION ALL SELECT s.catid, s.catname, s.parentcatid, levels = c.levels + 1 FROM cte c INNER JOIN @sample s ON c.catid = s.parentcatid)SELECT *FROM cte/*catid catname parentcatid levels ----------- --------------- ----------- ----------- 1 Cat1 0 02 Cat2 0 03 Cat3 0 08 Cat3_sub1 3 17 Cat2_sub1 2 19 Cat2_subsub 7 24 Cat1_sub1 1 15 Cat1_sub2 1 16 Cat1sub_sub 4 210 Cat2_subsub 6 3(10 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2009-10-17 : 02:39:17
|
| Thanks but i need the same way output by level and its sub categorycat1 level 0then its sub cat1's sub and its level 1then cat1's sub's sub category its level 2cat2 level 0then its sub.. level 1and so on... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-10-17 : 02:42:08
|
you mean the ordering of the result ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2009-10-17 : 02:45:38
|
| Yes khtan..as below which i have shown...here below.. level 0 and its cat1level 1 with cat1 's sublevel 3 and cat's sub 's sub.. nth LEVELcatid catname parentcatid --- levels1 --- Cat1 0 --- 04 --- Cat1_sub1 1 --- 16 --- Cat1sub_sub 4 --- 210 --- Cat1subsub_sub 6 --- 35 --- Cat1_sub2 1 --- 12 --- Cat2 0 --- 07 --- Cat2_sub1 2 --- 19 --- Cat2_subsub 7 --- 23 --- Cat3 0 --- 08 --- Cat3_sub1 3 --- 1 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-10-17 : 02:46:16
|
[code]; WITH cteAS( SELECT catid, catname, parentcatid, levels = 0, lvl = RIGHT('000' + CONVERT(varchar(MAX), catid), 3) FROM @sample WHERE parentcatid = 0 UNION ALL SELECT s.catid, s.catname, s.parentcatid, levels = c.levels + 1, lvl = c.lvl + RIGHT('000' + CONVERT(varchar(3), s.catid), 3) FROM cte c INNER JOIN @sample s ON c.catid = s.parentcatid)SELECT catid, catname, parentcatid, levelsFROM cteORDER BY lvl[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2009-10-17 : 02:54:08
|
| THANKS A LOT...i have previously tried out this same query using cursor.. but this time i am trying with different way..Thanks again.. |
 |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2009-10-17 : 06:23:18
|
| Hello khtan..i have 1 question that is..when i put varchar (50) and removing varchar(MAX)it gives error like..."Types don't match between the anchor and the recursive part in column "both have same datatype varchar still...and if i put varchar(MAX) it goes successfully..? |
 |
|
|
|
|
|
|
|