在 SQL(postgresql) 如何根据 "timestamp without time zone" 列进行分组?
In SQL(postgresql) How to group based on a "timestamp without time zone" column?
我正在尝试编写一些查询以根据 postgresql 中的每个月对事物进行分组。
假设我们有一个 table "crimes",它有 2 列 "activity date"(没有时区的时间戳)和 "zipcode"(字符变化 (5)),如何查询给定邮政编码的每个月的犯罪数量?
例如:
table "crimes":
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
输入:给定邮政编码“12345”
输出:return
month count
2014-10 2
2014-11 1
尝试:
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);
如何从 PostgreSQL 中的时间戳(with/out 时区)获取月份?
使用函数EXTRACT(field FROM source)
SELECT EXTRACT(MONTH FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 2
Link 到文档:https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
我正在尝试编写一些查询以根据 postgresql 中的每个月对事物进行分组。
假设我们有一个 table "crimes",它有 2 列 "activity date"(没有时区的时间戳)和 "zipcode"(字符变化 (5)),如何查询给定邮政编码的每个月的犯罪数量?
例如: table "crimes":
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
输入:给定邮政编码“12345”
输出:return
month count
2014-10 2
2014-11 1
尝试:
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);
如何从 PostgreSQL 中的时间戳(with/out 时区)获取月份?
使用函数EXTRACT(field FROM source)
SELECT EXTRACT(MONTH FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 2
Link 到文档:https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT