【问题标题】:Vertica. Count of Null and Not-Null of all columns of a Table垂直。表的所​​有列的 Null 和 Not-Null 计数
【发布时间】:2016-12-19 15:09:09
【问题描述】:

我们如何获得 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 来提供详细信息。

【问题讨论】:

    标签: sql vertica


    【解决方案1】:

    您好@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
    

    【讨论】:

    • 谢谢这是试图弄清楚的方法。你能告诉我 temp_sql_file 将包含什么。
    • 我尝试提供架构和表名。但我的 temp_sql_file 只包含 select abc.table as table_source from abc.table
    • temp_sql_file 应该包含您将要运行的完整 SQL 语句......它看起来类似于下面的 SQL。您可以使用 \i 命令运行它。 select 'public.TABLE_TEST' as table_source , sum(当 COL_01 不为 null 然后 1 else 0 end 的情况)作为 COL_01_NOT_NULL , sum(当 COL_01 为 null 然后 1 else 0 end 的情况)作为来自 public.TABLE_TEST 的 COL_01_NULL;
    • 如果你喜欢这个答案,那么你能接受这个解决方案吗? ;-)
    • @user2452689 -- 能否请您检查一下 SQL 参数中的大小写? “列”系统表中的选择语句依赖于完全匹配。如果表的大小写不同,这可以解释为什么您没有在“temp_sql_file”中获得任何记录...
    【解决方案2】:

    你可以使用:

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

    带有列名或表达式的count() 计算列/表达式中非NULL 值的数量。

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

    【讨论】:

      【解决方案3】:
      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-10-19
        • 1970-01-01
        • 1970-01-01
        • 2016-08-18
        • 2023-03-27
        • 2010-10-15
        • 2020-01-27
        相关资源
        最近更新 更多