I have a list of players who have hit Grand Slams this season, so far.
___________________________________________________
| Player Name | dateOfGrandSlam | distance | home |
---------------------------------------------------
| Griffin | 9-14-2013 | 413 | true |
| Griffin | 10-1-2013 | 371 | false|
| Simpson | 5-15-2013 | 413 | true |
| Reid | 7-1-2013 | 362 | true |
| Reid | 7-4-2013 | 363 | true |
| Reid | 9-28-2013 | 388 | true |
| Peavis | 8-14-2013 | 466 | false|
I want to get a list of players whose most recent grandslam was at home. If their most recent grandslam wasn't at home, I don't want them to show up on my list. This means, I need to select the player and group by the player and select the max date from that group. In that list, I also have to include home/away information so I can pick out the ones that were not at home.
However I'm having a problem. Because in order to select the attribute home
, I also need to include home
in the GROUP BY
clause.
For example:
SELECT playerName, MAX(date), distance, home
FROM grandslams
GROUP BY playerName, distance, home
The problem is that this returns a table with the most recent home and the most recent away grandslams.
| Player Name | dateOfGrandSlam | distance | home |
---------------------------------------------------
| Griffin | 9-14-2013 | 413 | true |
| Griffin | 10-1-2013 | 371 | false|
| Simpson | 5-15-2013 | 413 | true |
| Reid | 9-28-2013 | 388 | true |
This is not what I want - I want ONLY the most recent home grandslams IF there were no away grandslams more recently.
I want this result:
___________________________________________________
| Player Name | dateOfGrandSlam | distance | home |
---------------------------------------------------
| Simpson | 5-15-2013 | 413 | true |
| Reid | 9-28-2013 | 388 | true |
Essentially I need a way to perform the query that just gets me the most recent grandslam per player, tacks on the home
attribute (without having to group by it so I don't get his most recent home and his most recent away) and then can be easily filtered in an outer query.
In other words, I need to get the result from
SELECT playerName, MAX(date), distance
FROM grandSlams
GROUP BY playerName, distance
and attach the home
attribute to it.