2016-05-16 11 views
0

を持つテーブルをJOINの複数から複数のカウントを選択します。私は正しい結果取得以下のSQL持つ複数の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 

を私は結果(感想の数)がグループ化され、COMPANY_NAME、場所やad_messagesをサブグループます。

Impressions only Result

私はクリックのプロセスを繰り返し、その結果を得ることができます(クリック数)そうのような異なるフィルタによってグループ化され、サブグループ:

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 

しかし - 私は、インプレッション数とクリック数の両方を組み合わせたいと思います1つのクエリで、私は試しました:

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)は同じ値でした。 1つのSQLから正しい数を得る方法はありますか?

何か助けていただければ幸いです。ありがとう!

答えて

1

このようにcount(col_name) over (partition by .... order by ...)を使用すると、group byの代わりにselect distinctを使用できます。 full_table_nameの代わりにaliasを使用してみてください。

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

で試験
関連する問題