Я переписал весь запрос, пожалуйста попробуйте это:
select
q1.cnum,
case when col1_distincts>1 then null else col1_val end col1,
case when col2_distincts>1 then null else col2_val end col2,
case when col3_distincts>1 then null else col3_val end col3
from (
select cnum
from Table1
group by cnum
) q1
join (
select c1.cnum,
c1.col1_distincts, c1.col1_val,
c2.col2_distincts, c2.col2_val,
c3.col3_distincts, c3.col3_val
from (
select cnum, count(*) col1_distincts, max(col1) col1_val
from (
select cnum, col1
from Table1
group by cnum, col1
) group by cnum
) c1
join (
select cnum, count(*) col2_distincts, max(col2) col2_val
from (
select cnum, col2
from Table1
group by cnum, col2
) group by cnum
) c2 on c1.cnum=c2.cnum
join (
select cnum, count(*) col3_distincts, max(col3) col3_val
from (
select cnum, col3
from Table1
group by cnum, col3
) group by cnum
) c3 on c1.cnum=c3.cnum
) q2 on q1.cnum=q2.cnum
1-я попытка:
Я внес некоторые изменения в ваш подзапрос p1
, например, так:
Select distinct Table1.CNUM,
Case when qty1<>1 then null
else col1 END as col1,
Case when qty2<>1 then null
else col2 END as col2,
Case when qty3<>1 then null
else col3 END as col3
From Table1
Left join (
select CNUM, count(distinct col1) as qty1,
count(distinct col2) as qty2,
count(distinct col3) as qty3
FROM Table1
Group by CNUM
) p1
on p1.CNUM=Table1.CNUM
И я также учел различные значения для столбцов.
Ваш исходный подзапрос проверяет только более 1 записи на CNUM
.