我正在尝试编写一些查询来根据postgresql中的每个月对事物进行分组.
假设我们有一个表"罪行",它有2列"活动日期"(没有时区的时间戳)和"邮政编码"(字符变化(5)),如何查询给定邮政编码的每个月的犯罪数量?
例如:表"罪行":
activity date zipcode
2014-11-22 00:52:00 12345
2014-10-22 00:52:00 12345
2014-10-24 00:52:00 12345
2014-12-22 00:52:00 54321
Run Code Online (Sandbox Code Playgroud)
输入:给定的邮编"12345"
输出:返回
month count
2014-10 2
2014-11 1
Run Code Online (Sandbox Code Playgroud)
尝试:
select
extract(year from activity_date) as year,
to_char(activity_date, 'Mon') as month,
count(*) as count
from
crimes
group by
1,extract(month from activity_date);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8844 次 |
| 最近记录: |