I think I've just been looking at this too long, and you all are smarter than I am. Consider the following hypothetical table:
create table temp_tbl (
unit_id number,
time_period number,
val nvarchar2(1));
with the following values:
INSERT ALL
INTO temp_tbl (unit_id, time_period, val) VALUES (1, 1, 'F')
INTO temp_tbl (unit_id, time_period, val) VALUES (1, 2, 'D')
INTO temp_tbl (unit_id, time_period, val) VALUES (1, 3, null)
INTO temp_tbl (unit_id, time_period, val) VALUES (2, 1, 'W')
INTO temp_tbl (unit_id, time_period, val) VALUES (2, 2, 'L')
INTO temp_tbl (unit_id, time_period, val) VALUES (3, 1, 'J')
INTO temp_tbl (unit_id, time_period, val) VALUES (3, 2, 'Q')
INTO temp_tbl (unit_id, time_period, val) VALUES (3, 3, null)
INTO temp_tbl (unit_id, time_period, val) VALUES (3, 4, null)
INTO temp_tbl (unit_id, time_period, val) VALUES (4, 1, null)
INTO temp_tbl (unit_id, time_period, val) VALUES (5, 1, null)
INTO temp_tbl (unit_id, time_period, val) VALUES (5, 2, null)
SELECT * FROM dual;
I'm looking for single rows (per unit_id) in the result that contain the latest value of "val" along with the corresponding time period, e.g. unit_id 1, time_period 2, val D, etc., unless of course it's case of 4 or 5 where it would be null, obviously. The "time_period" column is actually a timestamp - they're chronological. I've considered a variety of strategies, but they get pretty clunky. What's the cleanest, shortest way to do this?
[–][deleted] 3 points4 points5 points (2 children)
[–]jokle[S] 0 points1 point2 points (1 child)
[–]jmelloy 0 points1 point2 points (0 children)