I am trying to combine multiple rows per company into one keeping the most recent column attribute value per row.
WITH COMPANY_DATA AS(
SELECT 1 ID, 'ABC Co.' COMPANY, NULL ATTR1, 'A' ATTR2, 'B' ATTR3, '8/1/2016 8:53.17 AM' ROW_UPDATED FROM DUAL UNION ALL
SELECT 2 ID, 'ABC Co.' COMPANY, 'F' ATTR1, NULL ATTR2, NULL ATTR3, '8/1/2016 8:55.19 AM' ROW_UPDATED FROM DUAL UNION ALL
SELECT 3 ID, 'ABC Co.' COMPANY, 'E' ATTR1, 'A' ATTR2, NULL ATTR3, '8/1/2016 8:55.23 AM' ROW_UPDATED FROM DUAL
)
SELECT COMPANY,
ATTR1,
ATTR2,
ATTR3
FROM COMPANY_DATA
From the above sample table I want to return:
COMPANY= ABC Co.
ATTR1 = E
ATTR2 = A
ATTR3 = B
Ooh, this is tricky. You can use keep
:
select company,
max(attr1) keep (dense_rank first order by (case when attr1 is not null then id end) desc nulls last) as attr1,
max(attr2) keep (dense_rank first order by (case when attr2 is not null then id end) desc nulls last) as attr2,
max(attr3) keep (dense_rank first order by (case when attr3 is not null then id end) desc nulls last) as attr3
from company_data cd
group by company;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.