1

mysql クエリで問題が発生しています。else if ステートメントで 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

上記のクエリは、次のように結果を取得します。

ここに画像の説明を入力

次のクエリを生成したい: 収益_タイプ = 4 の場合、値 sum_revenue = 値placement_id が変化する場合、以下の画像のようにしたい結果の例:

ここに画像の説明を入力

助けてくれてありがとう。

4

2 に答える 2

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
于 2013-03-20T19:44:29.503 に答える
1

SELECT case when m.revenue_type = 4 then d.campaignid else SUM(s.total_revenue) AS sum_revenue end, 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

于 2013-03-20T19:46:27.227 に答える