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
 General SQL Server Forums
 New to SQL Server Programming
 Order by

Author  Topic 

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-06-18 : 04:17:55
Hi how i put order by clause to get following output

id Parent_id Value

1 0 A
2 1 A1
3 1 A2
4 1 A3
5 0 B
6 5 B1
7 5 B2
8 5 B3

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-18 : 04:20:16
1. SELECT * FROM Table1 ORDER BY ID
2. SELECT * FROM Table1 ORDER BY Value

Or write a proper recursive CTE.

We have no idea on what sample data your above expected output is based on.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-06-18 : 04:24:08
sample data in the table should like this..

id parentid value
1 0 A
2 1 A1
3 5 B1
4 1 A2
5 0 B


Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-18 : 04:28:16
Ok, strike option #1 from my suggestion above.
Option #2 still works with your new provided sample data.

Or write a recursive CTE.


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-06-18 : 04:32:31
Hi peso,

sorry consider this as data

id Parent_id value

"1" "0" "Aquariums, zoos and museums"
"2" "1" "Aquariums"
"3" "1" "Art Museums"
"4" "1" "Science and nature museums"
"5" "1" "Zoos"
"6" "1" "Other museums"
"7" "1" "Classes and workshops"
"8" "1" "Other"
"9" "0" "Art"
"10" "76" "Ballet"
"11" "76" "Other dance"
"12" "76" "Musical theater"
"13" "76" "Opera"
"14" "76" "Puppet shows"
"15" "76" "Theater"
"16" "76" "Concerts"
"18" "9" "Classes and workshops"
"19" "9" "Other Art"
"20" "0" "Fairs and festivals"
"21" "0" "Food and cooking"
"22" "21" "Farmers market"
"23" "21" "Cooking classes"
"24" "21" "Other"
"25" "0" "Community action"
"26" "0" "Holiday events"
"27" "0" "Indoor fun"
"28" "0" "Books and language"
"29" "28" "Libraries"
"30" "28" "Bookstores"
"31" "28" "Storytelling"
"32" "28" "Language classes"
"33" "28" "Other"
"34" "0" "Movies"
"35" "0" "Outdoor adventures"
"36" "35" "Boats and ferries"
"37" "35" "Farms and ranches"
"38" "35" "Gardens"
"39" "35" "Hikes"
"40" "35" "Nature programs"
"41" "35" "Parks"
"42" "35" "Playgrounds"
"43" "35" "Other"
"44" "35" "Classes and workshops"
"45" "0" "Sports and movement"
"46" "45" "Baseball"
"47" "45" "Basketball"
"48" "45" "Football"
"49" "45" "Hockey"
"50" "45" "Other Sports"
"51" "45" "Classes and workshops"
"52" "51" "Basketball"
"53" "51" "Gymnastics"
"54" "51" "Martial arts"
"55" "51" "Skating"
"56" "51" "Skiing"
"57" "51" "Soccer"
"58" "51" "T-ball/baseball"
"59" "51" "Yoga"
"60" "0" "Swimming & splashing"
"61" "60" "Beachers"
"62" "60" "Lakes"
"63" "60" "Water parks"
"64" "60" "Pools"
"65" "60" "Other bodies of water"
"66" "60" "Classes"
"67" "0" "Parent's day out"
"68" "0" "Vehicles and Transportation"
"69" "68" "Automobiles"
"70" "68" "Boats"
"71" "68" "Buses"
"72" "68" "Planes"
"73" "68" "Trains"
"74" "68" "Trucks"
"75" "0" "City history and tours"
"76" "0" "Dance, music and theater"
"77" "9" "Art exhibits"
"78" "76" "Classes and workshops"
"79" "76" "Other"


Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-18 : 04:53:03
Thank you. Finally proper sample data.
And when proper sample data is displayed, the solution will be easy to make.
DECLARE	@Sample TABLE
(
ID INT,
ParentID INT,
Value VARCHAR(100)
)

INSERT @Sample
SELECT 1, 0, 'Aquariums, zoos and museums' UNION ALL
SELECT 2, 1, 'Aquariums' UNION ALL
SELECT 3, 1, 'Art Museums' UNION ALL
SELECT 4, 1, 'Science and nature museums' UNION ALL
SELECT 5, 1, 'Zoos' UNION ALL
SELECT 6, 1, 'Other museums' UNION ALL
SELECT 7, 1, 'Classes and workshops' UNION ALL
SELECT 8, 1, 'Other' UNION ALL
SELECT 9, 0, 'Art' UNION ALL
SELECT 10, 76, 'Ballet' UNION ALL
SELECT 11, 76, 'Other dance' UNION ALL
SELECT 12, 76, 'Musical theater' UNION ALL
SELECT 13, 76, 'Opera' UNION ALL
SELECT 14, 76, 'Puppet shows' UNION ALL
SELECT 15, 76, 'Theater' UNION ALL
SELECT 16, 76, 'Concerts' UNION ALL
SELECT 18, 9, 'Classes and workshops' UNION ALL
SELECT 19, 9, 'Other Art' UNION ALL
SELECT 20, 0, 'Fairs and festivals' UNION ALL
SELECT 21, 0, 'Food and cooking' UNION ALL
SELECT 22, 21, 'Farmers market' UNION ALL
SELECT 23, 21, 'Cooking classes' UNION ALL
SELECT 24, 21, 'Other' UNION ALL
SELECT 25, 0, 'Community action' UNION ALL
SELECT 26, 0, 'Holiday events' UNION ALL
SELECT 27, 0, 'Indoor fun' UNION ALL
SELECT 28, 0, 'Books and language' UNION ALL
SELECT 29, 28, 'Libraries' UNION ALL
SELECT 30, 28, 'Bookstores' UNION ALL
SELECT 31, 28, 'Storytelling' UNION ALL
SELECT 32, 28, 'Language classes' UNION ALL
SELECT 33, 28, 'Other' UNION ALL
SELECT 34, 0, 'Movies' UNION ALL
SELECT 35, 0, 'Outdoor adventures' UNION ALL
SELECT 36, 35, 'Boats and ferries' UNION ALL
SELECT 37, 35, 'Farms and ranches' UNION ALL
SELECT 38, 35, 'Gardens' UNION ALL
SELECT 39, 35, 'Hikes' UNION ALL
SELECT 40, 35, 'Nature programs' UNION ALL
SELECT 41, 35, 'Parks' UNION ALL
SELECT 42, 35, 'Playgrounds' UNION ALL
SELECT 43, 35, 'Other' UNION ALL
SELECT 44, 35, 'Classes and workshops' UNION ALL
SELECT 45, 0, 'Sports and movement' UNION ALL
SELECT 46, 45, 'Baseball' UNION ALL
SELECT 47, 45, 'Basketball' UNION ALL
SELECT 48, 45, 'Football' UNION ALL
SELECT 49, 45, 'Hockey' UNION ALL
SELECT 50, 45, 'Other Sports' UNION ALL
SELECT 51, 45, 'Classes and workshops' UNION ALL
SELECT 52, 51, 'Basketball' UNION ALL
SELECT 53, 51, 'Gymnastics' UNION ALL
SELECT 54, 51, 'Martial arts' UNION ALL
SELECT 55, 51, 'Skating' UNION ALL
SELECT 56, 51, 'Skiing' UNION ALL
SELECT 57, 51, 'Soccer' UNION ALL
SELECT 58, 51, 'T-ball/baseball' UNION ALL
SELECT 59, 51, 'Yoga' UNION ALL
SELECT 60, 0, 'Swimming & splashing' UNION ALL
SELECT 61, 60, 'Beachers' UNION ALL
SELECT 62, 60, 'Lakes' UNION ALL
SELECT 63, 60, 'Water parks' UNION ALL
SELECT 64, 60, 'Pools' UNION ALL
SELECT 65, 60, 'Other bodies of water' UNION ALL
SELECT 66, 60, 'Classes' UNION ALL
SELECT 67, 0, 'Parent''s day out' UNION ALL
SELECT 68, 0, 'Vehicles and Transportation' UNION ALL
SELECT 69, 68, 'Automobiles' UNION ALL
SELECT 70, 68, 'Boats' UNION ALL
SELECT 71, 68, 'Buses' UNION ALL
SELECT 72, 68, 'Planes' UNION ALL
SELECT 73, 68, 'Trains' UNION ALL
SELECT 74, 68, 'Trucks' UNION ALL
SELECT 75, 0, 'City history and tours' UNION ALL
SELECT 76, 0, 'Dance, music and theater' UNION ALL
SELECT 77, 9, 'Art exhibits' UNION ALL
SELECT 78, 76, 'Classes and workshops' UNION ALL
SELECT 79, 76, 'Other'

;WITH Yak (ID, Value, [Path], [Level])
AS (
SELECT ID,
Value,
CAST('/' + STR(ROW_NUMBER() OVER (ORDER BY Value), 4) + '/' AS VARCHAR(MAX)),
0
FROM @Sample
WHERE ParentID = 0

UNION ALL

SELECT s.ID,
s.Value,
y.[Path] + STR(ROW_NUMBER() OVER (ORDER BY s.Value), 4) + '/',
y.[Level] + 1
FROM Yak AS y
INNER JOIN @Sample AS s ON s.ParentID = y.ID
)

SELECT ID,
REPLICATE(' ', [Level]) + Value AS Header
FROM Yak
ORDER BY [Path]




E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-06-18 : 06:09:33
Hi i try this and got it...


select p.id,p.parent_id,p.description
,c.id,c.parent_id,c.description
from EVENT_CATEGORY p
inner join EVENT_CATEGORY c on c.parent_id=p.id
where p.parent_id=0 order by p.id, c.id


Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-18 : 06:29:54
Are you sure?
With your suggestion I cannot find

1. "Parent's day out"
2. "Martial arts"

Try my suggestion and you will find all 78 entries. With your suggestion I only get 54 entries.
Where are the missing 24 entries with your suggestion?

And as a sidenote, what's the point of asking for help here at SQLTeam
and then dismiss the working solution in favor of a non-working code?



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -