2013-03-20 65 views
1

我遇到了一個MySQL查詢的麻煩,我想選擇SUM,否則如果語句,這是我的故事。MySQL的查詢將SUM,但如果在其他領域的語句

我的查詢:

SELECT SUM(s.total_revenue) AS sum_revenue, m.revenue_type AS revenue_type, d.campaignid AS placement_id 
FROM ox_data_summary_ad_hourly AS s 
INNER JOIN ox_banners AS d ON (d.bannerid = s.ad_id) 
INNER JOIN ox_campaigns AS m ON (m.campaignid = d.campaignid) 
INNER JOIN ox_clients AS a ON (a.clientid = m.clientid) 
WHERE a.type = 0 
GROUP BY placement_id 
LIMIT 0 , 30 

而在上面的查詢得到的結果如下:

enter image description here

我要生成查詢,其中: 如果revenue_type = 4將值改爲sum_revenue =值placement_id, 示例結果我想要如下圖所示:

enter image description here

謝謝你的幫助。

回答

1

可以使用CASE表達式得到的結果:

SELECT 
    case 
     when m.revenue_type = 4 
     then d.campaignid 
     else SUM(s.total_revenue) end AS sum_revenue, 
    m.revenue_type AS revenue_type, 
    d.campaignid AS placement_id 
FROM ox_data_summary_ad_hourly AS s 
INNER JOIN ox_banners AS d 
    ON (d.bannerid = s.ad_id) 
INNER JOIN ox_campaigns AS m 
    ON (m.campaignid = d.campaignid) 
INNER JOIN ox_clients AS a 
    ON (a.clientid = m.clientid) 
WHERE a.type = 0 
GROUP BY placement_id 
LIMIT 0 , 30 
+0

好的,謝謝。解決了。 – 2013-03-20 19:47:39

+0

@LorenRamly很高興你得到它的工作 – Taryn 2013-03-20 19:48:04

+0

好吧感謝兄弟,我在玩openx :) – 2013-03-20 19:56:34

1

SELECT情況下m.revenue_type = 4,則d.campaignid其他SUM(s.total_revenue)AS sum_revenue結束,m.revenue_type AS revenue_type ,d.campaignid AS PLACEMENT_ID FROM ox_data_summary_ad_hourly爲S INNER JOIN ox_banners AS d ON(d.bannerid = s.ad_id) INNER JOIN ox_campaigns AS米ON(m.campaignid = d.campaignid) INNER JOIN ox_clients AS一個ON (a.clientid = m.clientid) WHERE a.type = 0 GROUP BY放置_id 限制0,30

+0

謝謝解決,但我使用上面的代碼,謝謝你的幫助。 – 2013-03-20 19:48:13

+0

對不起,我的代碼沒有格式化,因爲我沒有得到我的iPhone上的任何選項 – DevelopmentIsMyPassion 2013-03-20 19:49:26

+0

好吧,沒問題,謝謝兄弟。 – 2013-03-20 19:57:05