Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ordering with 'other'

Status
Not open for further replies.

mjd3000

Programmer
Apr 11, 2009
136
GB
I have a result set like this :

Other 34%
Dan 24%
Ken 17%
Claire 16%
Helen 9%

How do I other this so that the Other always shows as the last row, even if it isn't the lowest percentage?


 
Code:
DECLARE @Test TABLE (name varchar(20), Prc int)
INSERT INTO @Test VALUES ('Other',  34)
INSERT INTO @Test VALUES ('Dan'   , 24)
INSERT INTO @Test VALUES ('Ken'    ,17)
INSERT INTO @Test VALUES ('Claire' ,16)
INSERT INTO @Test VALUES ('Helen'  , 9)


SELECT Name, Prc
FROM @Test
ORDER BY Prc + CASE WHEN name = 'Other' Then 999 ELSE 0 END


or

Code:
DECLARE @Test TABLE (name varchar(20), Prc int)
INSERT INTO @Test VALUES ('Other',  34)
INSERT INTO @Test VALUES ('Dan'   , 24)
INSERT INTO @Test VALUES ('Ken'    ,17)
INSERT INTO @Test VALUES ('Claire' ,16)
INSERT INTO @Test VALUES ('Helen'  , 9)


SELECT Name, Prc
FROM @Test
ORDER BY CASE WHEN name = 'Other' Then 1 ELSE 0 END, Prc

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top