是否仍然只获取表中特定的行和列为空?
样本表
$ra
假设结果为:
PK COL1 COL2 COL3
1 AA BB CC
2 BB CC
3 AA
* AK是第一张桌子的PK。
我研究了这个:
https://dba.stackexchange.com/questions/28726/select-column-names-whose-entries-are-not-null
但遗憾的是,它不起作用。
答案 0 :(得分:1)
select PK, 'COL1' from sampleTable where COL1 IS NULL
UNION
select PK, 'COL2' from sampleTable where COL2 IS NULL
UNION
select PK, 'COL3' from sampleTable where COL3 IS NULL
你可以将它包装在rownum周围以获得你的第一列。
答案 1 :(得分:0)
下面的查询将返回具有空值和pk值的列名。您可以将此信息复制到其他表格。
select pk,nvl(col1,'col1') as columns_1 from table
where col1 is null
union all
select pk,nvl(col2,'col2') as columns_1 from table
where col2 is null
union all
select pk,nvl(col3,'col3') as columns_1 from table
where col3 is null
答案 2 :(得分:0)
这里是UNION ALL方法的替代方法,它可能(或可能不会)比多次从同一个表中读取更快:
with sample_data as (select 1 pk, 'AA' col1, 'BB' col2, 'CC' col3 from dual union all
select 2 pk, null col1, 'BB' col2, 'CC' col3 from dual union all
select 3 pk, 'AA' col1, null col2, null col3 from dual)
-- end of mimicking a table called sample_data with data in it
select row_number() over (order by sd.pk, d.id) pk, -- only for demo or reporting purposes; use a sequence.nextval for production code, if you're inserting this data somewhere.
sd.pk ak,
case when d.id = 1 then 'COL1'
when d.id = 2 then 'COL2'
when d.id = 3 then 'COL3'
end newcol
from sample_data sd
inner join (select level id
from dual
connect by level <= 3) d on ( (sd.col1 is null and d.id = 1)
or (sd.col2 is null and d.id = 2)
or (sd.col3 is null and d.id = 3));
PK AK NEWCOL
---------- ---------- ------
1 2 COL1
2 3 COL2
3 3 COL3