相关疑难解决方法(0)

查询滚动日期范围内的不同值的计数

我有一组电子邮件地址和日期,这些电子邮件地址已添加到表格中.对于不同的日期,可以有多个电子邮件地址条目.例如,如果我有下面的数据集.我希望得到我们在所述日期和3天前之间的不同电子邮件的日期和计数.

Date   | email  
-------+----------------
1/1/12 | test@test.com
1/1/12 | test1@test.com
1/1/12 | test2@test.com
1/2/12 | test1@test.com
1/2/12 | test2@test.com
1/3/12 | test@test.com
1/4/12 | test@test.com
1/5/12 | test@test.com
1/5/12 | test@test.com
1/6/12 | test@test.com
1/6/12 | test@test.com
1/6/12 | test1@test.com
Run Code Online (Sandbox Code Playgroud)

如果我们使用3的日期,结果集看起来会像这样

date   | count(distinct email)
-------+------
1/1/12 | 3
1/2/12 | 3
1/3/12 | 3
1/4/12 | 3
1/5/12 | 2
1/6/12 | 2
Run Code Online (Sandbox Code Playgroud)

我可以使用下面的查询获得日期范围的明确计数,但是希望按天计算范围,这样我就不必手动更新数百个日期的范围.

select test.date, count(distinct test.email)  
from test_table as test  
where test.date between '2012-01-01' and '2012-05-08' …
Run Code Online (Sandbox Code Playgroud)

sql postgresql date count

12
推荐指数
2
解决办法
2万
查看次数

标签 统计

count ×1

date ×1

postgresql ×1

sql ×1