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 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);

假定打开并单击已发送。

更新:为了性能,我们可以从以下查询开始:

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 回答
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 回答