I have a query which does a number of joins and has a few criteria in the WHERE clause, and I'm ending up with a result which essentially looks like this:
| userId | date | otherData |
|--------+------------+------------|
| 1 | 2008-01-01 | different |
| 1 | 2009-01-01 | info |
| 1 | 2010-01-01 | for |
| 2 | 2008-01-01 | each |
| 3 | 2008-01-01 | row |
| 3 | 2009-01-01 | here |
So, in essence for each user, there will be one or more dates in the past, and 0 or more dates in the future.
I need to somehow reduce the dataset to one row per user, only selecting the row which has the most recently passed date. That is, with whatever magic GROUP BY
or HAVING
clause is added, the result from above would look like this:
| userId | date | otherData |
|--------+------------+------------|
| 1 | 2009-01-01 | info |
| 2 | 2008-01-01 | each |
| 3 | 2009-01-01 | here |