#sql #count #pivot #amazon-redshift #aggregate-functions
#sql #подсчет #сводная #amazon-redshift #агрегатные функции
Вопрос:
В настоящее время у меня есть скрипт, который подсчитывает количество нулевых записей в каждом столбце. Сценарий приведен ниже
select sum(case when id is null then 1 else 0 end)id , sum(case when createddate is null then 1 else 0 end)createddate from schema_name.Table_name
Результаты выглядят так
|id|createddate|
|0 | 5 |
Есть ли способ перенести это, чтобы получить такие результаты, как
|id | 0 |
|created date| 5|
Наконец, если это возможно, смогу ли я присоединить это к информационной таблице (SVV_TABLE_INFO)
Ответ №1:
Вы можете использовать union all
:
select 'id', count(*)
from t
where id is null
union all
select 'createddate', count(*)
from t
where createddate is null;
Ответ №2:
Вы бы использовали union all
:
select 'id' as what, count(*) as cnt from table_name where id is null
union all
select 'createddate', count(*) from table_name where createddate is null
Если у вас возникла ситуация, когда оба столбца никогда null
не находятся в одной строке, вы можете использовать агрегацию:
select case when id is null then 'id' else 'createddate' end as what, count(*)
from mytable t
where id is null or createddate is null
group by case when id is null then 'id' else 'createddate' end