Question Overflow
Question Overflow

Reputation: 11255

Getting a Rank out of a Total

I have been doing this for quite some time:

SELECT COUNT(*) AS 'Rank' FROM Table
 WHERE Condition = 'Condition' AND Score >= 'Score';

SELECT COUNT(*) AS 'Total' FROM Table
 WHERE Condition = 'Condition';

Is there a more efficient way of getting both Rank and Total?

Upvotes: 2

Views: 174

Answers (1)

Martin Smith
Martin Smith

Reputation: 453298

You can calculate both at the same time with one pass through the data.

SELECT COUNT(*) AS 'Total', 
       SUM(CASE WHEN Score >= 'Score' THEN 1 ELSE 0 END) AS `Rank`
FROM Table
WHERE Condition = 'Condition';

Upvotes: 3

Related Questions