开发者

Using GROUP BY to Group Two Columns

开发者 https://www.devze.com 2023-03-30 06:05 出处:网络
I have the following query, and it produces a table. SELECT c.name AS campaign , c.traffic_source , ic.keyword

I have the following query, and it produces a table.

SELECT c.name AS campaign
  , c.traffic_source
  , ic.keyword
  , COUNT(ic.keyword) AS keycount
  , SUM(ls.amount) AS total_amount
  , ls.buyer AS buyers
  , ic.create_date AS 'date'
FROM in_clicks AS ic 
INNER JOIN ads AS a ON (ic.ad_id = a.id)
INNER JOIN ad_groups AS ag ON (a.ad_group_id = ag.id)
INNER JOIN campaigns AS c ON (ag.campaign_id = c.id)
INNER JOIN leads AS l ON (ic.id = l.in_click_id)
INNER JOIN lead_status AS ls ON (l.id = ls.lead_id)
WHERE keyword IS NOT NULL AND ic.create_date BETWEEN '2011-08-21' AND '2011-08-22'
      AND ic.location NOT LIKE 'Littleton%' AND ls.discriminator = 'AUTO_POST'
GROUP BY ic.keyword
ORDER BY keycount DESC

This query produces (abbreviated):

campaign             count    
Auto                 10
Auto Branded         8
Auto California      7 
Auto Branded         6
Auto                 2
Auto                 2 
Auto California      1

What I want to do is group the data according to both the 'count' and the c开发者_运维技巧ampaign name. So it would look like:

   campaign             count    
    Auto                 10
    Auto                 2
    Auto                 2 
    Auto Branded         8
    Auto Branded         6
    Auto California      7 
    Auto California      1

How can I get group the campaigns and counts together?


It looks like you actually want to order, not group, so just add campaign to order by, ie

ORDER BY campaign, keycount DESC


You just want to change the output order, so you should then order by campaign in the last line:

ORDER BY campaign ASC, keycount DESC
0

精彩评论

暂无评论...
验证码 换一张
取 消