gpt4 book ai didi

postgresql - 在 SQL(postgresql) 中如何根据 "timestamp without time zone"列进行分组?

转载 作者:行者123 更新时间:2023-11-29 11:32:49 26 4
gpt4 key购买 nike

我正在尝试编写一些查询来根据 postgresql 中的每个月对事物进行分组。

假设我们有一个表“crimes”,它有 2 列“activity date”(没有时区的时间戳)和“zipcode”(character varying(5)),如何在给定 zipcode 的情况下查询每个月的犯罪数量?

例如:表“犯罪”:

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”

输出:返回

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 - 在 SQL(postgresql) 中如何根据 "timestamp without time zone"列进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28039019/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com