2012-12-17 80 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,然後將該表連接到查詢中。