具有多个日期列的分组

时间:2013-03-15 13:22:16

标签: sql postgresql group-by

我正在尝试为电子邮件状态做报告,我需要每月保存记录。

这是我的数据表结构:

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

3 个答案:

答案 0 :(得分:1)

SQL Fiddle

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

答案 1 :(得分: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);

答案 2 :(得分: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)
;