我有一张桌子:
表格1
col1 col2 col3 col4
a b (null) c
a b (null) c
a (null) (null) c
(null) b (null) (null)
a b (null) (null)
a b (null) (null)
我的桌子上有大约300列。我需要查找每个非空列的值计数,而无需在表中键入每个列名称。
输出为:
column_name count_of_non_null
col1 5
col2 5
col3 0
col4 3
有没有办法做到这一点?
您需要动态的PL / SQL来编写条件聚合类型的查询:
select 'col1' col, count(case when col1 is null then 1 end) from table1
union all
select 'col2' col, count(case when col2 is null then 1 end) from table1
因此,您的PL / SQL代码将遵循以下原则
declare
v_cmd varchar2(10000);
begin
for c_column in (select column_name from user_tab_columns where table_name = 'table1') loop
v_counter := v_counter + 1;
v_cmd := v_cmd || 'select ''' || c_column.column_name || ''' col, count(case when ' || c_column.column_name || ' is null then 1 end) from table1 union all ';
end loop;
execute immediate left(v_cmd, length(v_cmd) - 11);
end;
/
我没有测试
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句