I have the next table:
Table SALES
Id | CustomerId | Date | NumItems |
---|---|---|---|
1 | 4 | 2023-01-14 | 2 |
2 | 4 | 2023-01-16 | 6 |
3 | 4 | 2023-01-10 | 9 |
I'm grouping the rows by CustomerId and I need to get the one whose date is the most recent:
SELECT
CustomerId,
MAX(Date),
NumItems <--- (?)
FROM SALES
GROUP BY CustomerId
But I also need to get number of items from the same row. How can I complement my query to get that column?
If the most recent registry is the second one (2023-01-16), I have to get the number of items from that row as well, which would be 6.