0

メールステータスのレポートを作成しようとしていますが、毎月記録を残す必要があります。

データのテーブル構造は次のとおりです。

sent_on date;  
sent boolean;   
open date;  
clicked date;

私が期待する最終的な出力は次のようになります。

Month  | Year   | Sent Count | Open Count | Clicked Count

次のような個別のステートメントを作成すれば、それができることを知っています。

Select MONTH(sent_on), YEAR(sent_on), count(sent_on) FROM my_table
WHERE sent = true;

Select MONTH(open), YEAR(open), count(open) FROM my_table;

Select MONTH(clicked ), YEAR(clicked ), count(clicked ) FROM my_table;

これについて私を助けて、簡単なアイデアを提供してもらえますか? 私は何かが欠けていると確信しています。psここでSQLを使用

4

3 に答える 3

1

SQL フィドル

select
    date_trunc('month', d) "month",
    count(sent and sent_on = d or null) sent_on,
    count(open = d or null) open,
    count(clicked = d or null) clicked
from
    t
    inner join
    generate_series(
        (select least(min(sent_on), min(open), min(clicked)) from t),
        (select greatest(max(sent_on), max(open), max(clicked))from t)
        , '1 day'
    ) s(d) on s.d in (t.sent_on, t.open, t.clicked)
group by 1
order by 1
于 2013-03-15T14:39:35.580 に答える
0

日付のピボットを解除し、結果を条件付きでグループ化します。

SELECT
  MONTH(eventdate) AS Month,
  YEAR (eventdate) AS Year,
  COUNT(CASE eventkind WHEN 'sent_on' THEN 1 END) AS SentCount,
  COUNT(CASE eventkind WHEN 'open'    THEN 1 END) AS OpenCount,
  COUNT(CASE eventkind WHEN 'clicked' THEN 1 END) AS ClickedCount
FROM (
  SELECT
    x.eventkind,
    CASE x.eventkind
      WHEN 'sent_on' THEN sent_on
      WHEN 'open'    THEN open
      WHEN 'clicked' THEN clicked
    END AS eventdate
  FROM my_table AS t
  CROSS JOIN (VALUES ('sent_on', 'open', 'clicked')) AS x (eventkind)
  WHERE t.sent = true
) AS s
GROUP BY
  MONTH(eventdate),
  YEAR (eventdate)
;
于 2013-03-15T16:47:51.043 に答える
0

必ずしもパフォーマンスの高いコードではない:

SELECT m, y, sent_count, open_count, clicked_count
FROM
    (SELECT MONTH(sent_on) as m, YEAR(sent_on) as y
    FROM my_table
    WHERE sent = true
    GROUP BY MONTH(sent_on), YEAR(sent_on)
    UNION ALL
    SELECT MONTH(open), YEAR(open) 
    FROM my_table
    WHERE sent = true
    GROUP BY MONTH(open), YEAR(open)
    UNION ALL
    SELECT MONTH(open), YEAR(open) 
    FROM my_table
    WHERE sent = true
    GROUP BY MONTH(open), YEAR(open)) d
LEFT JOIN 
(Select MONTH(sent_on) as m , YEAR(sent_on) as y, count(sent_on)  sent_count
 FROM my_table 
 WHERE sent = true
 GROUP BY MONTH(sent_on), YEAR(sent_on)) s on (s.y=d.y and s.m=d.m)
LEFT JOIN 
(Select MONTH(open) as m , YEAR(open) as y, count(open)  open_count
 FROM my_table 
 WHERE sent = true
 GROUP BY MONTH(open), YEAR(open)) o on (o.y=d.y and o.m=d.m)
LEFT JOIN 
(Select MONTH(clicked) as m , YEAR(clicked) as y, count(clicked)  clicked_count
 FROM my_table 
 WHERE sent = true
 GROUP BY MONTH(clicked), YEAR(clicked)) c on (c.y=d.y and c.m=d.m);

open と clicked が送信されたとします。

UPDATE : パフォーマンスのために、クエリに続くフォームを開始できます。

Select MONTH(sent_on) sm, YEAR(sent_on) sy, 
       MONTH(OPEN) om, YEAR(OPEN) oy, 
       MONTH(clicked) cm, YEAR(clicked) cy, 
       count(sent_on) sent_cnt, count(clicked) clicked_cnt, count(open) open_cnt
FROM my_table
WHERE sent = true
GROUP BY MONTH(sent_on), YEAR(sent_on), 
       MONTH(OPEN), YEAR(OPEN), 
       MONTH(clicked), YEAR(clicked);
于 2013-03-15T14:25:15.427 に答える