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:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old September 1st, 2004, 03:54 PM
sweetman sweetman is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 2 sweetman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
table select itself

is there a clever way to have a table do a select upon itself?
i've created a very simple table for tracking a threaded topic.
the initial message has a Parent ID = 0
the response message will have a Parent ID = the original messge.

what i'd like to do is list all messages IN ORDER, with the responses directly underneath them.

in the example below, i'd like to have record 3 appear below record 1; then record 2 and 5, then record 4...

table:
int ID
int ParentID

ID ParentID
1 0
2 0
3 1
4 0
5 2

Reply With Quote
  #2  
Old September 1st, 2004, 04:20 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,344 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 26 m 43 sec
Reputation Power: 891
Code:
select ID
     , ParentID 
  from yourtable
order 
    by case when ParentID=0 then ID else ParentID end
     , ID
if you had used NULL instead of 0, then you could say
Code:
order 
    by coalesce(ParentID,ID)
     , ID
__________________
r937.com | rudy.ca

Reply With Quote
  #3  
Old September 2nd, 2004, 11:16 AM
sweetman sweetman is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 2 sweetman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
beautiful.
thanks very much...

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > table select itself


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 5 hosted by Hostway