我有一个日期时间列'ts'(使用Amazon Redshift)。如何将数据分组30天。例如,如果我有这样的查询
select count(*) from table where ts > '2016-01-19' and ts < '2016-03-22'
如何将此分组为以下组而不明确指定这些日期
2016-01-19 to 2016-02-18
2016-02-19 to 2016-03-18
我已经忽略了2016-03-19之后的数据,因为一个月没有多少天?
有什么想法吗?
答案 0 :(得分:0)
像这样的东西
SELECT CASE
WHEN ts > '2016-01-19'
AND ts < '2016-02-18' THEN '2016-01-19 to 2016-02-18'
WHEN ts > '2016-02-19'
AND ts < '2016-03-18' THEN '2016-02-19 to 2016-03-18'
END,
Count(*)
FROM table
WHERE ts > '2016-01-19'
AND ts < '2016-03-22'
GROUP BY CASE
WHEN ts > '2016-01-19'
AND ts < '2016-02-18' THEN '2016-01-19 to 2016-02-18'
WHEN ts > '2016-02-19'
AND ts < '2016-03-18' THEN '2016-02-19 to 2016-03-18'
END