2016-05-16 77 views
0

我有以下SQL得到正確的結果表:選擇從多個多計數JOIN具有多個Group_BYs

SELECT 
companies.name as company_name, 
locations.country_code as location, 
ad_messages.ad_template_name as creative, 
impressions.width as width, 
impressions.height as height, 
count('impressions.id') as impressions 
FROM "impressions" 
INNER JOIN "ad_messages" ON "ad_messages"."id" = "impressions"."ad_message_id" 
INNER JOIN "visitors" ON "visitors"."id" = "ad_messages"."visitor_id" 
INNER JOIN "companies" ON "companies"."visitor_id" = "visitors"."id" 
INNER JOIN "locations" ON "locations"."visitor_id" = "visitors"."id" 
WHERE (visitors.user_id = 171) 
AND (impressions.created_at >= '2016-03-17 16:17:20.241276' 
AND impressions.created_at <= '2016-05-16 16:17:20.241336') 
GROUP BY 
companies.name, 
locations.country_code, 
ad_messages.ad_template_name, 
impressions.width, 
impressions.height 
ORDER BY 
companies.name, 
locations.country_code, 
ad_messages.ad_template_name, 
impressions.width, 
impressions.height 

我得到的結果(展示次數)分組並subgrouped即company_name,地點和ad_messages。

Impressions only Result

我可以重複此過程的點擊,並得到結果(點擊數)進行分組,並通過不同的過濾器subgrouped像這樣:

SELECT 
companies.name as company_name, 
locations.country_code as location, 
ad_messages.ad_template_name as creative, 
ad_clicks.width as width, 
ad_clicks.height as height, 
count('ad_clicks.id') as ad_clicks 
FROM "ad_messages" 
INNER JOIN "ad_clicks" ON "ad_clicks"."ad_message_id" = "ad_messages"."id" 
INNER JOIN "visitors" ON "visitors"."id" = "ad_messages"."visitor_id" 
INNER JOIN "companies" ON "companies"."visitor_id" = "visitors"."id" 
INNER JOIN "locations" ON "locations"."visitor_id" = "visitors"."id" 
WHERE (visitors.user_id = 171) 
AND (ad_clicks.created_at >= '2016-03-17 16:17:20.241276' 
AND ad_clicks.created_at <= '2016-05-16 16:17:20.241336') 
GROUP BY 
companies.name, 
locations.country_code, 
ad_messages.ad_template_name, 
ad_clicks.width, 
ad_clicks.height 
    ORDER BY 
companies.name, 
locations.country_code, 
ad_messages.ad_template_name, 
ad_clicks.width, 
ad_clicks.height 

但是 - 我想結合了展示和點擊在一個查詢中,我嘗試過:

SELECT 
companies.name as company_name, 
locations.country_code as location, 
ad_messages.ad_template_name as creative, 
impressions.width as width, 
impressions.height as height, 
ad_clicks.width as click_width, 
ad_clicks.height as click_height, 
count('impressions.id') as impressions, 
count('ad_clicks.id') as clicks 
FROM "ad_messages" 
INNER JOIN "impressions" ON "impressions"."ad_message_id" = "ad_messages"."id" 
INNER JOIN "visitors" ON "visitors"."id" = "ad_messages"."visitor_id" 
INNER JOIN "companies" ON "companies"."visitor_id" = "visitors"."id" 
INNER JOIN "locations" ON "locations"."visitor_id" = "visitors"."id" 
LEFT JOIN "ad_clicks" ON "ad_clicks"."ad_message_id" = "ad_messages"."id" 
WHERE (visitors.user_id = 171) 
AND (impressions.created_at >= '2016-03-17 16:17:20.241276' 
AND impressions.created_at <= '2016-05-16 16:17:20.241336') 
GROUP BY 
companies.name, 
locations.country_code, 
ad_messages.ad_template_name, 
impressions.width, 
impressions.height, 
ad_clicks.width, 
ad_clicks.height 

這給了我完全錯誤的結果!計數(展示次數)和次數(ad_clicks)具有相同的值。有什麼辦法可以從一個SQL獲得正確的計數嗎?

任何幫助表示讚賞。謝謝!

回答

1

您可以使用這樣的count(col_name) over (partition by .... order by ...),使用select distinct而不是group by。並嘗試使用alias而不是full_table_name

SELECT DISTINCT 
    companies.name as company_name, 
    locations.country_code as location, 
    ad_messages.ad_template_name as creative, 
    impressions.width as width, 
    impressions.height as height, 
    ad_clicks.width as click_width, 
    ad_clicks.height as click_height, 
    count('impressions.id') over (partition by companies.name, locations.country_code, 
      ad_messages.ad_template_name, impressions.width, impressions.height 
      order by companies.name, locations.country_code, ad_messages.ad_template_name, 
      impressions.width, impressions.height) as impressions, 
    count('ad_clicks.id') over (partition by companies.name, locations.country_code, 
      ad_messages.ad_template_name, ad_clicks.width, ad_clicks.height 
      order by companies.name, locations.country_code, ad_messages.ad_template_name, 
      ad_clicks.width, ad_clicks.height) as clicks 
FROM "ad_messages" 
INNER JOIN "impressions" ON "impressions"."ad_message_id" = "ad_messages"."id" 
INNER JOIN "visitors" ON "visitors"."id" = "ad_messages"."visitor_id" 
INNER JOIN "companies" ON "companies"."visitor_id" = "visitors"."id" 
INNER JOIN "locations" ON "locations"."visitor_id" = "visitors"."id" 
LEFT JOIN "ad_clicks" ON "ad_clicks"."ad_message_id" = "ad_messages"."id" 
WHERE (visitors.user_id = 171) 
    AND (impressions.created_at >= '2016-03-17 16:17:20.241276' 
    AND impressions.created_at <= '2016-05-16 16:17:20.241336') 
ORDER BY 
    companies.name, 
    locations.country_code, 
    ad_messages.ad_template_name, 
    impressions.width, 
    impressions.height, 
    ad_clicks.width, 
    ad_clicks.height 

SQLFIDDLE

的測試