别名枢轴列

时间:2020-06-03 10:56:00

标签: sql oracle plsql

我正在尝试将透视列的别名命名为方案1,方案2,方案3,而不是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, 

此查询作为一列出现,然后场景1、2,3应作为3列出现,其中基于campaign_id的pool_award值

2 个答案:

答案 0 :(得分:0)

只需使用条件聚合:

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;

答案 1 :(得分:0)

您可以在IN的{​​{1}}子句中使用别名,如下所示:

PIVOT
相关问题