ATMathew
ATMathew

Reputation: 12856

Using GROUP BY to Group Two Columns

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 campaign 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?

Upvotes: 0

Views: 243

Answers (2)

ain
ain

Reputation: 22749

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

ORDER BY campaign, keycount DESC

Upvotes: 3

Adriano Carneiro
Adriano Carneiro

Reputation: 58615

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

Upvotes: 2

Related Questions