【发布时间】:2021-08-06 16:47:05
【问题描述】:
我看到了很多关于如何在有列被查询的情况下执行此操作的解决方案,包括以下内容...
how to Replace null with zero in pivot SQL query
Oracle 11g SQL - Replacing NULLS with zero where query has PIVOT
Replacing null values in dynamic pivot sql query
等等,等等,等等,
但是,当您为条件的存在创建事实表时,如何替换数据透视查询中的空值。
例如,在 Databricks 中: 如何替换以下内容的空值
设置
drop table if exists patient_dx;
create table patient_dx (patient_id string, dx string);
insert into patient_dx values
('Bob', 'cough'),
('Donna', 'cough'),
('Jerry', 'cough'),
('Bob', 'feaver'),
('Donna', 'head ache')
;
查询:
select * from (
select
patient_id,
dx,
cast (1 as int) cnt
from
patient_dx
)
pivot (
max(cnt)
for dx in ('cough','feaver','head ache')
)
;
结果
我尝试了以下几种排列方式:
cast(0 + cast(coalesce(sum(coalesce(cnt,0)),0) as int) as int) as cnt
无济于事
【问题讨论】:
标签: apache-spark-sql pivot pivot-table databricks