create table LeadUnique as
select 1 as ColA,'a' as ColB,null as ColC,'J' as ColD,1 as ColE,'A' as ColF from dual
union select 1,'b',null,'K',2,'B' from dual
union select 2,'c',null,'L',2,'A' from dual
union select 2,'d','e' ,'M',3,'B' from dual
union select 2,'d','f' ,'N',3,'C' from dual;
--■■■冗長な列の組み合わせを排除しないクエリ■■■
with Temp as(
select distinct GA,GB,GC,GD,GE,GF
from (select ColA,ColB,ColC,ColD,ColE,ColF,
case grouping(ColA) when 0 then 'ColA' else ' ' end as GA,
case grouping(ColB) when 0 then 'ColB' else ' ' end as GB,
case grouping(ColC) when 0 then 'ColC' else ' ' end as GC,
case grouping(ColD) when 0 then 'ColD' else ' ' end as GD,
case grouping(ColE) when 0 then 'ColE' else ' ' end as GE,
case grouping(ColF) when 0 then 'ColF' else ' ' end as GF,
max(count(*))
over(partition by Grouping_ID(ColA,ColB,ColC,ColD,ColE,ColF)) as maxRecordCount
from LeadUnique
group by cube(ColA,ColB,ColC,ColD,ColE,ColF))
where maxRecordCount = 1)
select GA,GB,GC,GD,GE,GF from temp;
--■■■冗長な列の組み合わせを排除するクエリ■■■
with Temp as(
select distinct GID,GA,GB,GC,GD,GE,GF
from (select ColA,ColB,ColC,ColD,ColE,ColF,
Grouping_ID(ColA,ColB,ColC,ColD,ColE,ColF) as GID,
case grouping(ColA) when 0 then 'ColA' else ' ' end as GA,
case grouping(ColB) when 0 then 'ColB' else ' ' end as GB,
case grouping(ColC) when 0 then 'ColC' else ' ' end as GC,
case grouping(ColD) when 0 then 'ColD' else ' ' end as GD,
case grouping(ColE) when 0 then 'ColE' else ' ' end as GE,
case grouping(ColF) when 0 then 'ColF' else ' ' end as GF,
max(count(*))
over(partition by Grouping_ID(ColA,ColB,ColC,ColD,ColE,ColF)) as maxRecordCount
from LeadUnique
group by cube(ColA,ColB,ColC,ColD,ColE,ColF))
where maxRecordCount = 1)
select GID,GA,GB,GC,GD,GE,GF from temp a
where not exists(select 1 from temp b
where a.GID != b.GID
and BitAnd(a.GID,b.GID) = a.GID);