Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

first non-null value from a group with some ordering of rows

mNemAug 29 2019 — edited Aug 29 2019

I am trying get the first non-null value from some rows that belong to a group. How can I achieve it?

with t (id, grp, lbl) as

(

select 1, 9, null from dual union all

select 2, 9, null from dual union all

select 3, 9, 'p' from dual union all

select 4, 9, null from dual union all

select 5, 9, 'a' from dual union all

select 6, 9, 'd' from dual

)

select min(id) id,  min(lbl /* ignore nulls */)  keep (dense_rank first order by id) minlbl from t

group by grp

;

obviously, I cannot get the expected result from the above first aggregation query.

Expected result:

        ID MINLBL
---------- ------
         1 p     

version: 12.2.0.1.0

Message was edited by: mNem Modified the grp value to avoid the confusion after the post from @Ranagal.

This post has been answered by Solomon Yakobson on Aug 29 2019
Jump to Answer
Comments
Post Details
Added on Aug 29 2019
15 comments
3,796 views