MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 15th, 2004, 01:18 PM
sn1p3t sn1p3t is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 14 sn1p3t User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
How to implement this kind of sorting?

Here's a basic overview of my Table

ID identity int
ParentID int
Name varchar

Now, the root level items will have a ParentID of NULL, and the child nodes will have it's parent's ID in it's ParentID column (a simple self-referencing relationship).

However, when I select it, I would like to Order by ID, but then have all it's children right after it. For example, if this is the unsorted view:
Code:
ID     ParentID     Name
----------------------
1      <NULL>      Test
2     1               Test Child 1
3     <NULL>       Test 2
4     1                Test Child 2
5     3                 Test 2 Child 1

I would like the sorted to look like:
Code:
ID     ParentID     Name
----------------------
1      <NULL>      Test
2     1               Test Child 1
4     1                Test Child 2
3     <NULL>       Test 2
5     3                 Test 2 Child 1

Is this possible with my current structure? If not, how could I change it?

Reply With Quote
  #2  
Old July 15th, 2004, 01:32 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
give this a shot...simple order by should take care of this.
Code:
select	* from myTable
where	ParentID is not null
order 	by ParentID, name

...note, the where clause is included so that you don't get all the parent records back, just take it out if you wanna see 'em.

Reply With Quote
  #3  
Old July 15th, 2004, 01:34 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
Oh wait...you do want the NULL parentID's...sorry, my solution won't give you what you want...

Reply With Quote
  #4  
Old July 15th, 2004, 01:37 PM
sn1p3t sn1p3t is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 14 sn1p3t User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Figured it out:

Code:
SELECT ...
FROM ...
WHERE ...
ORDER BY COALESCE (ParentID, ID), ID


Easy when you use math

Reply With Quote
  #5  
Old July 15th, 2004, 01:51 PM
sn1p3t sn1p3t is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 14 sn1p3t User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Except if I wanted this to go down more than one level... still working on that

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > How to implement this kind of sorting?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway