Oracle SQL - 命名案例列

时间:2012-12-17 21:56:56

标签: sql oracle

select site,
case
when site='AppCircle' then (count(create_dtime)*0.4438083264)
when site='AppCircle Clips' then (count(create_dtime)*0.0096978792)
when site='BC : SponsorPay' then (count(create_dtime)*0.9620989399)
when site='BonusCoins.com : Aarki' then (count(create_dtime)*0.4612565445)
when site='Nielsen Rewards' then (count(create_dtime)*-0.6000000000)
when site ='Portal : Paymentwall' then (count(create_dtime)*0.5433541667)
when site ='Portal : RadiumOne' then (count(create_dtime)*0.0619798753)
when site ='Portal : TrialPay' then (count(create_dtime)*2.1468159204)
when site ='bonuscp_login' then (count(create_dtime)*-0.1500000000)
when site ='facebook_like' then (count(create_dtime)*2.1468159204)
when site ='iTunes' then (count(create_dtime)*-0.0300000000)
end
From player_aux_pt
Where
Trunc(Create_Dtime) >= To_Date('2012-Nov-01','yyyy-mon-dd')
And Trunc(Create_Dtime) <= To_Date('2012-Nov-30','yyyy-mon-dd')  
group by site

导致两列数据

Site    [insert every case statement here]

我只想将第二列信息命名为“Profit”

Site    Profit

我尝试了许多不同的方法。

2 个答案:

答案 0 :(得分:3)

您可以使用AS:

在SQL中重命名(或命名)列和表达式
CASE 
  WHEN. . .
  WHEN. . .
END AS Profit

但是,以这种方式使用CASE表达式并不是非常可扩展的。考虑将乘法因子移动到另一个表,键入site,然后将该表连接到查询中。

答案 1 :(得分:3)

注意:由于您的CASE语句只是基本的DECODE模式,请查看链接以获得简洁的替代方案。

select site,
       count(create_dtime)
     * DECODE(site, 'AppCircle', 0.4438083264,
                    'AppCircle Clips', 0.0096978792,
                    'BC : SponsorPay', 0.9620989399,
                    ......) Profit
 ....

<小时/> 您可以通过在表达式或基本列名称后面为其命名来对别名进行别名,例如

SELECT
    Site,
    Site ReNamedSite,
    Concat(Site,'a') "AddedAnA",
    COALESCE(Site,Address) AS "Two Words"
...

注意:

  1. 关键字AS是可选的
  2. 双引号的使用是可选的,除非您使用多个单词
  3. select site,
    case
    when site='AppCircle' then (count(create_dtime)*0.4438083264)
    when site='AppCircle Clips' then (count(create_dtime)*0.0096978792)
    when site='BC : SponsorPay' then (count(create_dtime)*0.9620989399)
    when site='BonusCoins.com : Aarki' then (count(create_dtime)*0.4612565445)
    when site='Nielsen Rewards' then (count(create_dtime)*-0.6000000000)
    when site ='Portal : Paymentwall' then (count(create_dtime)*0.5433541667)
    when site ='Portal : RadiumOne' then (count(create_dtime)*0.0619798753)
    when site ='Portal : TrialPay' then (count(create_dtime)*2.1468159204)
    when site ='bonuscp_login' then (count(create_dtime)*-0.1500000000)
    when site ='facebook_like' then (count(create_dtime)*2.1468159204)
    when site ='iTunes' then (count(create_dtime)*-0.0300000000)
    end Profit
    From player_aux_pt
    Where
    Trunc(Create_Dtime) >= To_Date('2012-Nov-01','yyyy-mon-dd')
    And Trunc(Create_Dtime) <= To_Date('2012-Nov-30','yyyy-mon-dd')  
    group by site