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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Rank() But move 0 values to bottom of Rank?

Status
Not open for further replies.

Brianfree

Programmer
Feb 6, 2008
220
GB
Hi, is it possible to rank the Price in ascending order but have all 0 Prices still ranked last? At the moment the 0 Prices are returning as Rank 1 as they are the lowest value?

Example Data

User,Price,Rank
abc,0,5
def,0,5
ghi,1,1
jkl,4,4
mno,5,3
pqr,3,2

Many thanks
Brian
 
What's your SQL to create your outcome?

[pre]
User Price Rank
abc 0 5
def 0 5
ghi 1 1
jkl 4 4
mno 5 3
pqr 3 2
[/pre]
I would just 'fake' another column with my own Price (MyPrice):

[pre]
SELECT
User,
Price,[blue]
CASE WHEN Price = 0 THEN 99
ELSE Price
END As MyPrice[/blue]
RANK () OVER (
ORDER BY [blue]MyPrice[/blue] DESC
) my_rank
FROM
MyTableName;
[/pre]

---- Andy

There is a great need for a sarcasm font.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top