2016-12-19 4 views
2

Verticaのテーブルのすべての列のNULLとNULL以外のカウントを取得するにはどうすればよいですか?表はn個の列を持つことができ、各列に対してその表のNULLとNULL以外の値の数を取得する必要があります。Vertica。テーブルのすべての列のヌルとノンヌルの数

たとえば、表の下 は、私がprojection_storageなどのようなシステムテーブルをチェックし

特定の列は、その後、我々はCOLUMN2ため

SELECT COUNT(*) FROM table where column1 is null; 
SELECT COUNT(*) FROM table where column1 is not null; 

同じクエリのように確認することができ、その場合は2列

column1 Column2 
1   abc 
      pqr 
3 
      asd 
5 

を持っているが、私は数字をカントTABLE NAMEのみをハードコーディングして詳細を与える一般的なクエリを照会する。

を使用でき

答えて

1

こんにちはする@ user2452689:ここではN列にヌル&ないヌルをカウントするあなたの条件を満たしている動的に生成VSQL文です。一時的なSQLファイルを作業ディレクトリに書き出し、\ iコマンドで実行することに注意してください。テーブルごとに最初の2つの変数を変更するだけです。これが役立つことを願って - 幸運! :-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 
+0

ありがとうございましたこれはアプローチを把握しようとしていた。 temp_sql_fileに含まれるものを教えてください。 – user2452689

+0

私はスキーマとテーブル名を提供することによって試しました。しかし、私のtemp_sql_fileには、abc.tableからtable_sourceとして選択されたabc.tableしか含まれていません。 – user2452689

+0

temp_sql_fileには、実行する完全なSQL文が含まれている必要があります。 \ iコマンドで実行します。 (COL_01がnullでないときに他の1 0端場合)public.TABLE_TESTからCOL_01_NULL として(場合COL_01次いで他0,1エンドヌルである場合)、COL_01_NOT_NULL として、table_source として和を合計 'public.TABLE_TEST' を選択; –

0

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

count()列名または式では、列/式の非NULL値の数をカウントします。

(もちろん、NULL値の数がcnt - cnt_columnXです。)

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 
関連する問題