2011-08-24 86 views
0

我有以下查詢,它會生成一個表。使用GROUP BY分組到兩列

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 

,該查詢產生(略):

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

我想要做的是根據雙方的「計數」和活動名稱的數據組。 因此,它看起來像:

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

我怎樣才能獲得小組活動和統計在一起?

回答

3

它看起來像你真的想訂購,不羣,所以只需添加campaign訂購通過,即

ORDER BY campaign, keycount DESC 
+0

謝謝...運動不應該有DESC,但除此之外,該訣竅 – ATMathew

+0

有趣的是,我貼我的回答你做1個min13s之前。 :) –

+0

嗯,我認爲這就像我們最初發布時間相距幾秒鐘......但我已upvoted你的答案,如果這是任何安慰:) – ain

2

你只是想改變的輸出順序,所以你應該再爲了通過競選的最後一行:

ORDER BY campaign ASC, keycount DESC