I'm on postgres 9.4. I want to query my adjustments
table for the latest adjustment for each field. Some adjustments
can adjust multiple fields.
Example 1
+---------------+-------------+----------------+---------------+
| available_qty | on_hand_qty | backstock_qty | date |
+---------------+-------------+----------------+---------------+
| null | 2 | null | 2018-01-03 |
| 1 | null | 10 | 2018-01-04 |
| null | null | 2 | 2018-01-01 |
| 3 | 3 | null | 2018-01-02 |
+---------------+-------------+----------------+---------------+
...and querying for the latest adjustment for each field would result in:
+---------------+-------------+----------------+---------------+
| available_qty | on_hand_qty | backstock_qty | date |
+---------------+-------------+----------------+---------------+
| null | 2 | null | 2018-01-03 |
| 1 | null | 10 | 2018-01-04 |
+---------------+-------------+----------------+---------------+
Example 2
+---------------+-------------+----------------+---------------+
| available_qty | on_hand_qty | backstock_qty | date |
+---------------+-------------+----------------+---------------+
| null | 2 | null | 2018-01-03 |
| 1 | null | null | 2018-01-04 |
| null | null | 2 | 2018-01-01 |
| null | null | 3 | 2018-01-02 |
| 3 | null | null | 2018-01-02 |
| null | 4 | null | 2018-01-01 |
+---------------+-------------+----------------+---------------+
...and querying for the latest adjustment for each field would result in:
+---------------+-------------+----------------+---------------+
| available_qty | on_hand_qty | backstock_qty | date |
+---------------+-------------+----------------+---------------+
| null | 2 | null | 2018-01-03 |
| 1 | null | null | 2018-01-04 |
| null | null | 3 | 2018-01-02 |
+---------------+-------------+----------------+---------------+
I can achieve this with a lot of UNION
queries:
(SELECT * FROM adjustments WHERE available_qty IS NOT NULL ORDER BY date DESC LIMIT 1)
UNION
(SELECT * FROM adjustments WHERE on_hand_qty IS NOT NULL ORDER BY date DESC LIMIT 1)
UNION
(SELECT * FROM adjustments WHERE backstock_qty IS NOT NULL ORDER BY date DESC LIMIT 1)
...but is this achievable with one query? In reality, there can be lots of fields here. Thanks in advance!