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!

Getting one record in a group if columns are null

sgalaxyJan 25 2021

Hi,
we have the following records:
with sample_data(m, s, k) as
(select '10', '10', 'a' from dual union all
select '10', '20', 'a' from dual union all
select '15', '10', 'b' from dual union all
select '16', '10', 'a' from dual union all
select '16', '16', 'b' from dual union all
select '10', null, null from dual union all
select '20', '20', 'b' from dual union all
select '20', null, null from dual union all
select '20', null, null from dual )
select * from sample_data

now we want to get the following result set:

M S K
-----------------------
10 NULL NULL
15 10 b
16 10 a
16 16 b
20 NULL NULL

In other words if for the columns S, K there are null then
this record to be fetched(once when there multiple records) but not the others which have the same
value on column M but not null values on S, K columns (as for the recs with M column value: 10).
If the are no null for columns S, K then all recs to be fetched
(as for the recs with M value: 15, 16)

I tried dense_rank to get the first in ach group of recs (with null on cols S, K) but i could not get
the desired result.

Note: I use oracle db10g v.2
Thanks,
Sim

This post has been answered by mathguy on Jan 25 2021
Jump to Answer
Comments
Post Details
Added on Jan 25 2021
6 comments
1,142 views