I have a table similar to this:
product_id | client_id | category | price | created_date |
---|---|---|---|---|
1 | 1 | A | 3.1 | 2022-11-01 |
2 | 1 | A | 3.2 | 2022-11-02 |
3 | 1 | B | 3.3 | 2022-11-03 |
4 | 1 | B | 3.4 | 2022-11-04 |
5 | 2 | B | 3.5 | 2022-11-05 |
6 | 2 | B | 3.6 | 2022-11-06 |
7 | 2 | A | 3.7 | 2022-11-07 |
8 | 2 | C | 3.8 | 2022-11-08 |
And I want to select the price from the latest created_date from each client_id and category, so my expected result would be this:
product_id | client_id | category | price | created_date |
---|---|---|---|---|
2 | 1 | A | 3.2 | 2022-11-02 |
4 | 1 | B | 3.4 | 2022-11-04 |
6 | 2 | B | 3.6 | 2022-11-06 |
7 | 2 | A | 3.7 | 2022-11-07 |
8 | 2 | C | 3.8 | 2022-11-08 |
Could you please help me with this? Thanks
I found something similar here: Select first row in each GROUP BY group?
UPDATE
Actually I want to do the same with the following (this is a more realistic example): https://www.db-fiddle.com/f/fHc6MafduyibJdkLHe9cva/0
Expected result:
val1 | val2 | num1 | num2 | created_date |
---|---|---|---|---|
X | A | 33 | 333 | 2022-11-03 |
X | B | 66 | 666 | 2022-11-06 |
X | C | 88 | 888 | 2022-11-08 |
X | D | 99 | 999 | 2022-11-09 |
Y | A | 111 | 1111 | 2022-11-11 |
3
Answers
You can do in this way.
Use MAX to get the latest date first and group by category and client ID.
Check out this db_fiddle
Please provide DDL + DML commands next time when you ask question so that others don’t need to prepare DDL and DML commands by their own. It is advisable to share what you’ve tried as well.
written in postgresql standard. It may helpful for you.
Tested in Postgresql DB. it is working as expected.
If your
MySQL
version is 8.0 or higher,then we can use windows function to do itDB Fiddle Demo