I have a table named tbl_user_complete
and a query like this:
user_id | bill_id | complete_date |
---|---|---|
user_1 | bill_1 | 2024-01-14 |
user_1 | bill_2 | 2024-02-14 |
user_1 | bill_3 | 2024-05-01 |
user_2 | bill_4 | 2024-01-03 |
SELECT user_id
, bill_id
, MAX(complete_date) AS complete_date
FROM tbl_user_complete
GROUP BY user_id;
I want to select users having max complete_date
and group by user_id
only, and expect to result like below:
user_id | bill_id | complete_date |
---|---|---|
user_1 | bill_3 | 2024-05-01 |
user_2 | bill_4 | 2024-01-03 |
But when I run my above query, it said the query needed to be grouped by bill_id
also, which returns pretty much a copy of the original table.
How should I fix the query to get the desired result ?
3
Answers
If you want to select a whole row which has a max of something, you need to use window functions to partition and order the rows, then select the first in each partition.
Demonstration
Consider self-joining unit level to aggregate level to query data based on aggregate value:
2024-05-01
This will work but you won’t get the bill_id. As soon as you add "bill_id" for selection it must be part of GROUP BY. and as bill ID is unique in your example it splits to each row separate.