我有一个类似[data in long format]的配置单元数据>
我想对其进行重组以使其看起来像data in wide format
我的外观如下my latest attempt
这是我使用的代码。我想摆脱所有NULL,并将所有月份合并为一年以下的一行。
select carrier, year, month,
max(case when month =1 then quantity end) as jan,
max(case when month =2 then quantity end) as feb,
max(case when month =3 then quantity end) as mar,
max(case when month =4 then quantity end) as apr,
max(case when month =5 then quantity end) as may,
max(case when month =6 then quantity end) as jun,
max(case when month =7 then quantity end) as jul,
max(case when month =8 then quantity end) as aug,
max(case when month =9 then quantity end) as sep,
max(case when month =10 then quantity end) as oct,
max(case when month =11 then quantity end) as nov,
max(case when month =12 then quantity end) as dec
from (select final_month.*, row_number() over
(partition by carrier, year order by carrier, year) from final_month)
final_month group by carrier, year, month;
谢谢!
我有一个蜂巢数据,看起来像是长格式的数据,我想将其重构为看起来像是宽格式的数据,这是我最近的尝试,这是我的代码...
从选择和分组依据中删除month
。而且我相信您想要的是SUM,而不是max(),但是您当然更了解数据: