别名数据透视列

pbwdgjma  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(244)

我正在尝试将透视列别名为scenario1、scenario2、scenario3,而不是1、2、3。我犯了个错误。

select *
from (select *
      from (select s.campaign_id campaign_id, s.scenario_index scenario_index 
            from scenario s, campaign c where s.campaign_id = c.campaign_id)
      pivot (max(scenario_index)for scenario_index in (1,2,3))
     )a

谢谢,聚合现在用别名给出结果。我的要求是将这些列与另一个

select  CASE WHEN AWARD_TYPE = 0 THEN award_rate||' points'
                                        when AWARD_TYPE = 1   then Award_rate||' %'
                                        when award_type=2  then RATIO_POINTS||' points per '||RATIO_MON_UNIT||' AED' End
                            from  points_rule p
                            where c.pt_basic_rule_id = p.point_rule_id ) as pool_awards,

此查询以列形式出现,然后scenario1、2、3应以3列形式出现,其中包含基于活动id的pool\u award的值

cmssoen2

cmssoen21#

只需使用条件聚合:

select s.campaign_id,
       max(case when scenario_index = 1 then 1 end) as scenario1,
       max(case when scenario_index = 2 then 1 end) as scenario2,
       max(case when scenario_index = 3 then 1 end) as scenario3
from scenario s join
     campaign c 
     on s.campaign_id = c.campaign_id
group by campaign_id;
jm2pwxwz

jm2pwxwz2#

可以在中使用别名 IN 合同条款 PIVOT 具体如下:

select *
from (select *
      from (select s.campaign_id campaign_id, s.scenario_index scenario_index 
            from scenario s, campaign c where s.campaign_id = c.campaign_id)
      pivot (max(scenario_index)for scenario_index in (1 as scenario1,2 as scenario2,3 as scenario3))
     )a

相关问题