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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to sort a table

Author  Topic 

Rauken
Posting Yak Master

108 Posts

Posted - 2012-01-11 : 10:15:09
Hi!

I have a table that I want to sort/group in a certain way. To make it simple it looks like this:

id, name, extraid

Example:
1, 'Michael', null
2, 'John', null
3, 'Lee', 1
4, 'John', 2

I want to sort it first on extraid that has a null value then the rows that are connected to that id through the extraid.

Hope you understand what I mean.

/M

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-01-11 : 10:51:41
If it has just the two levels you indicated, it probably can be done via a self-join, but I suspect what you are asking is how to sort a hierarchy. If that is so, you would need what they refer to as materialized path. Here is a way of doing it - to show what I am trying to do, I constructed a test table; in your real query, you wouldn't need the test table.

You may also want to invnestigate the use of hierarchyid data type.
CREATE TABLE #tmp(id INT, extraid INT);
INSERT INTO #tmp VALUES (1,NULL),(2,NULL),(3,2),(4,2),(5,1),(6,5),(7,4),(8,3),(9,3),(10,4);

;WITH cte AS
(
SELECT
*,
CAST(ROW_NUMBER() OVER (ORDER BY id) AS VARCHAR(max)) AS MPath,
1 AS lvl
FROM
#tmp
WHERE
extraid IS NULL

UNION ALL

SELECT
t.id,
t.extraid,
CAST(c.MPath+'/'+CAST(ROW_NUMBER() OVER (ORDER BY t.id) AS VARCHAR(255)) AS VARCHAR(MAX)),
c.lvl+1
FROM
cte c
INNER JOIN #tmp t ON t.extraid = c.ID
)
SELECT * FROM cte ORDER BY MPath;

DROP TABLE #tmp;
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-01-11 : 11:10:46
Sunita..."why so serious"..ummm I mean complicated

ORDER BY CASE WHEN extraid IS NULL THEN [id] ELSE extraid END

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-01-11 : 11:19:26
quote:
Originally posted by X002548

Sunita..."why so serious"..ummm I mean complicated

ORDER BY CASE WHEN extraid IS NULL THEN [id] ELSE extraid END

Brett

8-)

Don'tcha know it? I like to write the most complicated queries ever! I am going for the Guinness record
Go to Top of Page

Rauken
Posting Yak Master

108 Posts

Posted - 2012-01-12 : 04:48:01
Thanks for your help. It's almost there. Brett I want the null row to come first then the rows "connected" to that row below now it's the opposite.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-01-12 : 05:36:04
quote:
Originally posted by sunitabeck

quote:
Originally posted by X002548

Sunita..."why so serious"..ummm I mean complicated

ORDER BY CASE WHEN extraid IS NULL THEN [id] ELSE extraid END

Brett

8-)

Don'tcha know it? I like to write the most complicated queries ever! I am going for the Guinness record


You will never win!

The most complicated query that you (as an experienced sql developer )

Will NEVER BE as complicated as the nested cursor horror show written by a novice.

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Rauken
Posting Yak Master

108 Posts

Posted - 2012-01-12 : 06:45:38
Forget what I wrote, it works fine! Thanks!!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-01-12 : 14:06:20
quote:
Originally posted by Rauken

Forget what I wrote, it works fine! Thanks!!



Which one?

How about

ORDER BY CASE WHEN extraid IS NULL THEN 0 ELSE 1 END
, CASE WHEN extraid IS NULL THEN [id] ELSE extraid END

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

Rauken
Posting Yak Master

108 Posts

Posted - 2012-01-12 : 17:04:57
Brett your first solution works fine :-)
Go to Top of Page
   

- Advertisement -