You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@kylin.apache.org by 欧秋斌 <ou...@163.com> on 2020/04/28 07:42:10 UTC

关于union all之后,字段数值会产生null值的问题

您好!


我的工作中,由于订单类型的维度标准不是确定的,所以需要在SQL语句中进行定义(如下方第一个公用表达式)。
在下面的语句中,进行union all拼接时, 第二个维度的销售额(val_day)值会出现null。但是单独执行订单类型和日期这两个维度中的任何一个都可以返回结果。维度两者union all时会出问题。
我想了没弄明白,这个问题出在哪里 ?


with cte as( -- 每天和每种订单类型的销售额汇总 
select part_dt, 
       case when lstg_format_name='Auction' then '一类订单'
           when lstg_format_name='FP-non GTC' then '二类订单'
           when lstg_format_name='ABIN' then '三类订单'
           when lstg_format_name='FP-GTC' then '四类订单'
           else '其他订单' end style,
       sum(price)  val 
from kylin_sales_ts2
group by part_dt, case when lstg_format_name='Auction' then '一类订单' when lstg_format_name='FP-non GTC' then '二类订单' when lstg_format_name='ABIN' then '三类订单' when lstg_format_name='FP-GTC' then '四类订单' else '其他订单' end
)
,cte2 as( ----  分别按照订单类型和日期维度 
select part_dt, sum(val)over(partition by style) val_style,  sum(val)over(partition by part_dt) val_day  from cte
where part_dt>='201310'
)
---- 两种维度结果分别去重后,合并起来  
select part_dt, val_style  from cte2  ---- 订单类型维度 
group by part_dt, val_style


union all
select part_dt, val_day is  from cte2 ---- 日期维度 
group by part_dt, val_day