垂直。 Table 的所有列的 Null 和 Not-Null 计数

Vertica. Count of Null and Not-Null of all columns of a Table

我们如何获取 Vertica 中 Table 的所有列的空和非空计数? Table 可以有 n 列,对于每一列,我们需要获取 table.

的空值和非空值的计数

例如。 Table 下面有两列

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 的相同查询

我检查了 table 系统,例如 projection_storage 和其他系统,但我无法找出一个通用查询,该查询仅通过在查询中硬编码 TABLE NAME 来提供详细信息。

您可以使用:

select count(*) as cnt,
       count(column1) as cnt_column1,
       count(column2) as cnt_column2
from t;

count() 与列名或表达式计算 column/expression.

中非 NULL 值的数量

(很明显,NULL个值的个数是cnt - cnt_columnX。)

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

你好@user2452689:这是一个动态生成的 VSQL 语句,它满足您计算 N 列中的空值和非空值的要求。请注意,这会将一个临时 SQL 文件写入您的工作目录,然后通过 \i 命令执行它。您只需要更改每个 table 的前两个变量。希望这有帮助,祝你好运! :-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