2012-12-17 75 views
1
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 

导致两列数据的Oracle SQL - 命名的情况下,列

Site [insert every case statement here] 

我只是想,信息第二列被命名为“利润”

Site Profit 

我有尝试了许多不同的方式,我卡住了。

+0

的搜索case语法你'重新使用会更好,因为一个简单的例子,“CASE SITE当'AppCircle'然后......当'AppCircle Clips'然后......”。实际上,将站点值和常量放置到另一个表中并加入到其中会更好。 Create_Dtime列上的TRUNC不是很好的做法 - 最好将其删除并用<

回答

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. 用法是可选的,除非你使用多个单词

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 
2

可以使用AS重命名SQL(或名称)列和表达式:

CASE 
    WHEN. . . 
    WHEN. . . 
END AS Profit 

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