PostgreSQL中对日期时间进行分组

PostgreSQL 在PostgreSQL中对日期时间进行分组|极客教程 (geek-docs.com)

# 按年月日时分组

SELECT extract(year from created_time) as year, extract(month from created_time) as month, extract(day from created_time) as day, extract(hour from created_time) as hour, count(*) as count
FROM sc_app
GROUP BY extract(year from created_time), extract(month from created_time), extract(day from created_time), extract(hour from created_time)
ORDER BY extract(year from created_time), extract(month from created_time), extract(day from created_time), extract(hour from created_time);

 # 按年月日分组

SELECT extract(year from created_time) as year, extract(month from created_time) as month, extract(day from created_time) as day, count(*) as count
FROM sc_app
GROUP BY extract(year from created_time), extract(month from created_time), extract(day from created_time)
ORDER BY extract(year from created_time), extract(month from created_time), extract(day from created_time);

 # 按date分组

select date(created_time) as date, count(*) as count from sc_app group by date(created_time);

 

posted @   你说夕阳很美  阅读(125)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2023-10-18 数据库设计使用id做查询条件的好处
2023-10-18 virtualbox虚机读取宿主机USB设备配置
点击右上角即可分享
微信分享提示