I have a table in a PostgreSQL database with data like:
id customer_id item value timestamp
1 001 price 1000 11/1/2021
2 001 price 1500 11/2/2021
3 001 condition good 11/3/2021
4 002 condition bad 11/4/2021
5 002 condition good 11/5/2021
6 002 price 1000 11/6/2021
7 001 condition good 11/7/2021
8 001 price 1400 11/8/2021
9 002 price 1500 11/9/2021
10 001 condition ok 11/10/2021
11 002 price 1600 11/11/2021
12 002 price 1550 11/12/2021
From this table, I want to query the latest value by date and convert it to a table as below.
customer_id price condition
001 1400 ok
002 1550 good
To get this kind of table I tried with this below query, but it did not work well when there are too much data. (Operation like Min and Max to text and number)
I tested this in pgAdmin 4:
SELECT customer_id,
MAX (Case WHEN item='price' THEN value END) price,
MAX (Case WHEN item='condition' THEN value END) condition
FROM table_name GROUP BY customer_id
I want to query the value by the latest date updated data.