2016-08-17 4 views
-2

日付に基づいていくつかのメトリックを集約しようとしています。問題は、一致する関連付けのある日付のみが選択されることです。私は日付のフルレンジを望んでいます。私はこの右ジョインでNULLを選択しない

SELECT f2."day", coalesce(count(c0."id"), 0) 
FROM "conversations" AS c0 
LEFT OUTER JOIN "apps" AS a1 ON a1."id" = c0."app_id" 
RIGHT OUTER JOIN ((SELECT now()::date - d AS day FROM generate_series (0, 10) d)) AS f2 ON f2."day" = c0."inserted_at"::date 
WHERE (a1."id" = 'ASnYW1-RgCl0I') 
GROUP BY f2."day" 
+1

があなたの 'WHERE'句があなたの'のアプリケーションから任意の 'NULL'sの可能性を排除しています'テーブル。 – Nicarus

+0

http://wiki.lessthandot.com/index.php/WHERE_conditions_on_a_LEFT_JOIN – HLGEM

答えて

3

にしようとしています

参加条件のあなたのwhere句の一部ではなくてください。以下のコードは、左に変わり、このコードにはない

SELECT f2."day", coalesce(count(c0."id"), 0) 
FROM "conversations" AS c0 
LEFT OUTER JOIN "apps" AS a1 ON a1."id" = c0."app_id" 
RIGHT OUTER JOIN ((SELECT now()::date - d AS day FROM generate_series (0, 10) d)) AS f2 ON f2."day" = c0."inserted_at"::date 
WHERE (a1."id" = 'ASnYW1-RgCl0I') 
GROUP BY f2."day" 

に参加し、内側にアプリに参加する:

SELECT f2."day", coalesce(count(c0."id"), 0) 
FROM "conversations" AS c0 
LEFT OUTER JOIN "apps" AS a1 ON a1."id" = c0."app_id" and (a1."id" = 'ASnYW1-RgCl0I') 
RIGHT OUTER JOIN ((SELECT now()::date - d AS day FROM generate_series (0, 10) d)) AS f2 ON f2."day" = c0."inserted_at"::date 
GROUP BY f2."day" 
+1

また、「WHERE(合体(a1。 "id"、 'ASnYW1-RgCl0I')= 'ASnYW1-RgCl0I') ' – Hogan

関連する問題