Oracle SQL: Exists, Distinct (Multiple Columns Combo), Order BY

56 views Asked by At

Oracle SQL: (Select all columns based on unique value with combination of column: activity + column: r_system that has the latest column: end_date)

Sample Data

https://i.stack.imgur.com/mJPCq.png

Attempt:

SELECT T1.* FROM happy T1 WHERE EXISTS ( SELECT DISTINCT T2.activity,T2.r_system, MAX(T2.END_DATE)

FROM happy T2

WHERE T1.activity=T2.activity,T1.r_system=T2.r_system, T1.END_DATE=T2.END_DATE

GROUP BY T2.activity, T2.r_system

ORDER BY MAX(T2.END_DATE) DESC,T2.r_system, T2.activity )

1

There are 1 answers

0
user21755023 On

Credit to: How to grab the last value in a column per user for the last date

Result:

select * from happy where ( activity, r_system, END_DATE ) in ( select activity, r_system, max(END_DATE) from happy group by activity, r_system ) order by activity, r_system, END_DATE