I have a table in MySql 5.7, where the names are repeated according to the date. I need to recover the first 2 records for each name.
example:
name year month
xxxx 2019 8
xxxx 2019 7
xxxx 2019 6
xxxx 2019 5
.... .... ..
zzzz 2019 5
zzzz 2019 4
zzzz 2019 3
zzzz 2019 2
.... .... ..
expected result:
name year month
xxxx 2019 8
xxxx 2019 7
zzzz 2019 5
zzzz 2019 4
other ...
I need to retrieve the first two records for each name, it is not valid to do so with date clauses.
the query I try to emulate:
SELECT
name, year, month
FROM (
SELECT
*,
row_number() OVER (PARTITION BY name ORDER BY year DESC, month DESC)
FROM
table
) a
WHERE row_number <= 2
thanks.