1

I am having trouble with a mysql query, I want to select SUM with else if statement, this is my story.

My query:

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

And query in above get result as below:

enter image description here

I want to generate query which: If revenue_type=4 will change in value sum_revenue = value placement_id, Example result I want to as image below:

enter image description here

Thanks for help me.

2 Answers 2

1

You can use a CASE expression to get the result:

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
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

Thanks solved too, But I using code from above, thanks for help me.
Sorry my code not formatted as i dont get any option on my iphone

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.