Solved

ID and parentID - how to sort?

Posted on 2002-06-21
3
434 Views
Last Modified: 2006-11-17
Hi, this is a table I've got (if it doesn't look good, paste it into notepad and use system as font... )

tblProjects
-----------------------------------
|  ID  |   parentID   |  content  |
|----------------------------------
|   1  |     null     |  blabla   |
|   2  |     1        |  blabla   |
|   3  |     1        |  blabla   |
|   4  |     null     |  blabla   |
|   5  |     4        |  blabla   |
|   6  |     4        |  blabla   |
|   7  |     4        |  blabla   |
|   8  |     null     |  blabla   |
|   9  |     1        |  blabla   |
|  10  |     1        |  blabla   |
|  11  |     4        |  blabla   |
|  12  |     4        |  blabla   |
-----------------------------------

These are all different projects and instances of projects. Where ID=1, we have a Mother project. ID 2 and ID 3 are children of that Mother Project. Same is found for Mother project 4 and mother project 8.

I want to send a request to the database whereas it replies with the entire list; listing first the mother project, then the parent projects, under each mother object. The result would be something like this:

-----------------------------------
|  ID  |   parentID   |  content  |
|----------------------------------
|   1  |     null     |  blabla   |
|   2  |     1        |  blabla   |
|   3  |     1        |  blabla   |
|   9  |     1        |  blabla   |
|  10  |     1        |  blabla   |
|   4  |     null     |  blabla   |
|   5  |     4        |  blabla   |
|   6  |     4        |  blabla   |
|   7  |     4        |  blabla   |
|  11  |     4        |  blabla   |
|  12  |     4        |  blabla   |
|   8  |     null     |  blabla   |
-----------------------------------

Get my picture? How can I do this?

Any help appreciated,

Kind regards,
Simon
0
Comment
Question by:NiTech
3 Comments
 
LVL 39

Expert Comment

by:appari
ID: 7098406
select ID, case parentID when null then ID else parentID end, content  from tblProjects
order by 2,1
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 100 total points
ID: 7098407
This is the way to do it:
select SortID = case when parentid is null then id else parentid end, ID, ParentID , Data
from yourtable
order by 1,2

Now, don't be disturbed by the SortID column for output, you should simply ignore it in the Application. If you still want to supress it:

select ID, ParentID, Data from
(select SortID = case when parentid is null then id else parentid end, ID, ParentID , Data
from yourtable) l
select SortID = case when parentid is null then id else parentid end, ID, ParentID , Data
from yourtable
order by SortID, ID

CHeers

0
 

Author Comment

by:NiTech
ID: 7102700
Thank you- this worked excellent.

This is an example of my output:

SortID      ID          ParentID    enDescription                                              
----------- ----------- ----------- ----------------------
195         195         NULL        Autonomy Implementation
226         226         NULL        Additional HelpDeks resource Q1
230         230         NULL        Accenture
235         235         NULL        Alcatel 4980 Implementation
296         296         NULL        Application Support/ Upgrade OOS
348         348         NULL        ATM Workshop
354         354         NULL        Annual leave
354         653         354         Annual Leave Calloff 1
354         654         354         Annual Leave Calloff 1
354         655         354         Annual Leave Calloff 1
361         361         NULL        Application Upgrade - item 001
402         402         NULL        Autonomy

As you can see, it is now very easy to determine whether or not the row is a child or a parent, simply by checking for existance of a ParentID.

Thank you very much.

Kind Regards,
Simon
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
backup job space check 4 40
incorrect syntax near the order by 10 39
Need help with T-SQL on SQL Server 2014 9 37
Help With SQL Query 8 25
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question