Steven Duncan
Steven Duncan

Reputation: 39

SQL: How to get Max of Variable (per category of another variable)

Situation: I have a table containing the data field my_number and my_location (among many other data fields). I'm only concerned about those two fields. I want to select the maximum 'my_number' for each 'my_location'. There are only 3 possible my_location options but numerous possible my_number. All variables represented by numbers.

I was researching correlated subquery but I do not know how to use them. Maybe joins could work? I am trying to learn different ways, effective ways to complete this task.

Upvotes: 2

Views: 79

Answers (2)

Aldrin
Aldrin

Reputation: 766

Try this:

SELECT 
     MAX(my_number) as Max_Number,
     my_location
FROM 
     TABLE
GROUP BY 
     my_location

Upvotes: 1

HoneyBadger
HoneyBadger

Reputation: 15150

SELECT  my_location
,       MAX(my_number) my_number_max
FROM    My_Table
GROUP BY my_location

See: MAX, GROUP BY

Upvotes: 1

Related Questions