I have data which I have to 'group by'. In each resultant group, there are rows with multiple columns which has to be treated as such: for each such given column, return non-null, most current value. So I have to 'group by'(gb) whole table, and find 'max-like(NUM)' for every column(below represented as NUM). max-like function sorts by temporal column, below represented as 'time'. In another words, group by 'gb', sort group by 'time' desc nulls last, get first item in group.
Sorry, for that convoluted description. I hope it's clear. Any idea how to write that sql query (oracle/postgres)?
example
CREATE TABLE test (
gb integer,
NUM INTEGER,
time integer
);
--rows=groups, columns=time; so in first row=group data
--sorted by time are from left to right the middle value
--in triplet, thus, 2,1,3. Ie. most current non-null value in time is 3.
insert into test VALUES (1,2,1),(1,1,2),(1,3,3);--3
insert into test VALUES (2,1,1),(2,2,2),(2,3,3);--3
insert into test VALUES (3,3,1),(3,2,2),(3,1,3);--1
insert into test VALUES (4,3,1),(4,2,2),(4,null,3);--2
insert into test VALUES (5,2,1),(5,3,2),(5,null,3);--3
insert into test VALUES (6,2,1),(6,null,2),(6,null,3);--2
query
select
t.gb,
'<magic goes here>'
from test t
GROUP BY t.gb ORDER BY t.gb;
is expected to return
1 | 3
2 | 3
3 | 1
4 | 2
5 | 3
6 | 2
<magic goes here>should produce.nulls? should you still shownullin the output?