I'm inserting a new row per order update so I can keep track of the state updates. Like this.
+----------+-----------+--------+-----------------------+-----------------------+----------------------------------+
| event_id | state | amount | address | notes | timestamp |
+----------+-----------+--------+-----------------------+-----------------------+----------------------------------+
| order123 | fulfilled | NULL | NULL | NULL | 2022-07-01T17:08:12.032316+00:00 |
| order123 | NULL | NULL | NULL | Delivered to customer | 2022-07-01T17:07:12.032316+00:00 |
| order123 | NULL | NULL | 300 Post St, CA 94108 | NULL | 2022-07-01T17:06:12.032316+00:00 |
| order123 | accepted | NULL | NULL | NULL | 2022-07-01T17:05:12.032316+00:00 |
| order123 | pending | 100 | NULL | NULL | 2022-07-01T17:04:12.032316+00:00 |
+----------+-----------+--------+-----------------------+-----------------------+----------------------------------+
How would I write a query that pulls out the latest value from each of those columns, ignoring the NULLs. This is what I'm looking for.
+----------+-----------+--------+-----------------------+-----------------------+----------------------------------+
| event_id | state | amount | address | notes | timestamp |
+----------+-----------+--------+-----------------------+-----------------------+----------------------------------+
| order123 | fulfilled | 100 | 300 Post St, CA 94108 | Delivered to customer | 2022-07-01T17:08:12.032316+00:00 |
+----------+-----------+--------+-----------------------+-----------------------+----------------------------------+
This is as far as I've gotten, which gives me the latest record:
SELECT DISTINCT ON (event_id)
event_id, state, amount, address, notes, timestamp
FROM event_order
ORDER BY event_id, timestamp DESC;
DB Fiddle link: https://www.db-fiddle.com/f/pZFUG96oYh6ymAz99vvqWh/0
I came across this post which mentions LAST_VALUE
but the solution seems like it only works with integers.
Any help will be appreciated.