Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get MAX int but exclude specific int?

I'd like to get the max integer from a specific column excluding a value that will always be the max if present. Data may look like the following:

score, empid
 1       3
 3       3
 10      3
 1       5 
 2       5
 1       8
 2       8
 3       8
 10      8

In the above, I'd like MAX score less than 10. MAX(score) doesn't work in this case since it will bring back 10. Results should look like this:

score, empid
 3       3
 2       5
 3       8

Any suggestions?

like image 700
4thSpace Avatar asked Dec 05 '22 17:12

4thSpace


1 Answers

Here's an alternative method:

SELECT
   MAX(CASE WHEN score = 10 THEN NULL ELSE score END) AS [max_score],
   empid
FROM
   table
GROUP BY
   empid

This may be preferable if you prefer to avoid the sub-select.

like image 157
Ben English Avatar answered Dec 08 '22 04:12

Ben English