Unix Technical Forum

sorting table

This is a discussion on sorting table within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi friends, ID LASTNAME --------+---------------------------- 2 FFF 1 XXX 0 CCC 1 DDD 2 BBB 0 EEE 0 GGG ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 03:04 PM
Oonz
 
Posts: n/a
Default sorting table

Hi friends,


ID LASTNAME
--------+----------------------------
2 FFF
1 XXX
0 CCC
1 DDD
2 BBB
0 EEE
0 GGG
3 III
3 HHH
4 ZZZ
4 ZZZ


please help me in sorting this table. the table should be sorted based
on LASTNAME, and then the ID (only non-zero id should be sorted).


ID LASTNAME
--------+----------------------------
2 BBB
2 FFF
0 CCC
1 DDD
1 XXX
0 EEE
0 GGG
3 HHH
3 III
4 XYZ
4 ZZZ

should be sorted based on last name and should be grouped with the
same ID, except for the ID as 0

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 03:04 PM
Plamen Ratchev
 
Posts: n/a
Default Re: sorting table

Please don't multipost, rather crosspost. Replied in
microsoft.public.sqlserver.server:

SELECT F.ID,
F.LASTNAME
FROM Foobar AS F
LEFT OUTER JOIN
(
SELECT ID,
MIN(LASTNAME) AS GRP
FROM Foobar
WHERE ID > 0
GROUP BY ID
) AS G
ON F.ID = G.ID
ORDER BY COALESCE(G.GRP, F.LASTNAME),
F.ID,
F.LASTNAME

HTH,

Plamen Ratchev
http://www.SQLStudio.com



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 03:05 PM
Oonz
 
Posts: n/a
Default Re: sorting table

Hi Plamen,
It was really great.Thanks for your immediate reply.

Regards,
Arunkumar.Dharuman





On May 15, 5:54 pm, "Plamen Ratchev" <Pla...@SQLStudio.com> wrote:
> Please don't multipost, rather crosspost. Replied in
> microsoft.public.sqlserver.server:
>
> SELECT F.ID,
> F.LASTNAME
> FROM Foobar AS F
> LEFT OUTER JOIN
> (
> SELECT ID,
> MIN(LASTNAME) AS GRP
> FROM Foobar
> WHERE ID > 0
> GROUP BY ID
> ) AS G
> ON F.ID = G.ID
> ORDER BY COALESCE(G.GRP, F.LASTNAME),
> F.ID,
> F.LASTNAME
>
> HTH,
>
> Plamen Ratchevhttp://www.SQLStudio.com



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:42 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com