【问题标题】:aliasing pivot column别名数据透视列
【发布时间】:2020-06-03 10:56:00
【问题描述】:

我正在尝试将数据透视列别名为方案 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 列形式出现,其中 pool_award 的值基于campaign_id

【问题讨论】:

  • 你为什么选择不使用正确的、明确的、标准的、可读的JOIN语法?
  • 您有几个选项可以在 pivot 中添加别名,例如here
  • 提供样本数据和期望的结果。

标签: sql oracle plsql


【解决方案1】:

只使用条件聚合:

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;

【讨论】:

  • 谢谢,它给出了与列别名相同的结果,我的要求是将这些列与另一个查询结合起来选择 CASE WHEN AWARD_TYPE = 0 THEN Award_rate||'积分'当 AWARD_TYPE = 1 然后 Award_rate||' %' 当 Award_type=2 时 RATIO_POINTS||'每个“||RATIO_MON_UNIT||”的点数AED' End from points_rule p where c.pt_basic_rule_id = p.point_rule_id ) as pool_awards
【解决方案2】:

您可以在PIVOTIN 子句中使用别名,如下所示:

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 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 2016-02-03
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多