Redshift Postgres SQL 比较 table 中的 NULL 与 NOT NULL 值

Redshift Postgres SQL comparing NULL vs NOT NULL values in a table

我正在尝试在 Redshift DB (Postgres SQL) 中创建查询以执行以下操作:

我正在检查一些列以进行质量控制,并且需要每列的 NULLNOT NULL 的百分比。我希望我的输出看起来像这样,下面显示了总数,但如果可能的话需要在 % 中。我该如何编写此查询?

Column     NOT NULL  NULL       Total Records           Percentage NULL   
--------- -------   ------    ----------------        ---------------------
Column A     78       10            88                       11.3%
Column B     68       15            83                       18.0%
Column C      3        5             8                       62.5%

使用 SQL,您可以计算特定列的值,如下所示:

select
  count(a) as "NOT_NULL",
  count(*) - count(a) as "NULL",
  count(*) as "Total Records",
  to_char(100.0 * (count(*) - count(a)) / count(*), '999.9%') as "Percentage NULL"
from stack

但是无法显示"one row per column"。您必须 JOIN 多个查询一起才能产生该结果。