如何在Vertica中获取表的所有列的null和非null计数?表可以有n列,每列需要计算该表的空值和非空值。
例如。 下表有两列
column1 Column2
1 abc
pqr
3
asd
5
如果是特定列,那么我们可以检查
SELECT COUNT(*) FROM table where column1 is null;
SELECT COUNT(*) FROM table where column1 is not null;
对column2的相同查询
我检查了像projection_storage和其他系统表这样的系统表,但是我找不到一个通用查询,它通过在查询中仅使用TABLE NAME进行硬编码来提供详细信息。
答案 0 :(得分:1)
您可以使用:
select count(*) as cnt,
count(column1) as cnt_column1,
count(column2) as cnt_column2
from t;
带有列名或表达式的 count()
计算列/表达式中非NULL
值的数量。
(显然,NULL
值的数量为cnt - cnt_columnX
。)
答案 1 :(得分:1)
Hello @ user2452689:这是一个动态生成的VSQL语句,它满足您计算空值的要求。 N列中不为空。请注意,这会将临时SQL文件写入您的工作目录,然后通过\ i命令执行它。您只需要更改每个表的前两个变量。希望这有帮助,祝你好运! :-D
--CHANGE SCHEMA AND TABLE PARAMETERS ONLY:
\set table_schema '\'public\''
\set table_name '\'dim_promotion\''
---------
\o temp_sql_file
\pset tuples_only
select e'select \'' || :table_schema || e'\.' || :table_name || e'\' as table_source' as txt
union all
select * from (
select
', sum(case when ' || column_name || ' is not null then 1 else 0 end) as ' || column_name || '_NOT_NULL
, sum(case when ' || column_name || ' is null then 1 else 0 end) as ' || column_name || '_NULL' as txt
from columns
where table_schema = :table_schema
and table_name = :table_name
order by ordinal_position
) x
union all
select ' from ' || :table_schema || e'.' || :table_name || ';' as txt ;
\o
\pset tuples_only
\i temp_sql_file
答案 2 :(得分:0)
select column1_not_null
,column2_not_null
,column3_not_null
,cnt - column1_not_null as column1_null
,cnt - column2_not_null as column2_null
,cnt - column3_not_null as column3_null
from (select count(*) as cnt
,count (column1) as column1_not_null
,count (column2) as column2_not_null
,count (column3) as column3_not_null
from mytable
) t