|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
Code:
select ID
, ParentID
from yourtable
order
by case when ParentID=0 then ID else ParentID end
, ID
Code:
order
by coalesce(ParentID,ID)
, ID
|
|
#3
|
|||
|
|||
|
beautiful.
thanks very much... |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > table select itself |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|