这篇文章介绍了PostgreSQL实现按年、月、日、周、时、分、秒分组统计的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
按年查询
select to_char(date::DATE, 'YYYY') as year,sum(shares) as shares, sum(visits) as visits
from database_table
where date >= '2019-01-01' and date <= '2020-01-01' group by year order by year
按月查询
select to_char(date::DATE, 'YYYY-MM') as month,sum(shares) as shares, sum(visits) as visits
from database_table
where date >= '2019-01-01' and date <= '2020-01-01' group by month order by month
按周查询
select to_char(date::DATE-(extract(dow from date::TIMESTAMP)-1||'day')::interval, 'YYYY-mm-dd') week,
sum(shares) as shares, sum(visits) as visits
from database_table
where date >= '2019-01-01' and date <= '2020-01-01' group by week order by week
按天查询
select to_char(date::DATE, 'YYYY-MM-DD') as day,sum(shares) as shares, sum(visits) as visits
from database_table
where date >= '2019-01-01' and date <= '2020-01-01' group by day order by day
按小时查询
select to_char(date::DATE, 'YYYY-MM-DD HH24') as hour,sum(shares) as shares, sum(visits) as visits
from database_table
where date >= '2019-01-01' and date <= '2020-01-01' group by hour order by hour
按分钟查询
select to_char(date::DATE, 'YYYY-MM-DD HH24:MI ') as minute,sum(shares) as shares, sum(visits) as visits
from database_table
where date >= '2019-01-01' and date <= '2020-01-01' group by minute order by minute
按秒查询
select to_char(date::DATE, 'YYYY-MM-DD HH24:MI:SS ') as second,sum(shares) as shares, sum(visits) as visits
from database_table
where date >= '2019-01-01' and date <= '2020-01-01' group by second order by second
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:PostgreSQL实现按年、月、日、周、时、分、秒的分组统计
基础教程推荐
猜你喜欢
- Sql Server Management Studio连接Mysql的实现步骤 2023-07-29
- 如何将excel表格数据导入postgresql数据库 2023-07-20
- Mysql查询所有表和字段信息的方法 2023-07-26
- python中pandas库的iloc函数用法解析 2023-07-28
- 关于MySQL中explain工具的使用 2023-07-27
- Python常见库matplotlib学习笔记之多个子图绘图 2023-07-27
- SQLServer 清理日志的实现 2023-07-29
- Redis如何实现延迟队列 2023-07-13
- Mysql主从三种复制模式(异步复制,半同步复制,组复 2022-09-01
- 【Redis】数据持久化 2023-09-12