PostgreSQL实现按年、月、日、周、时、分、秒的分组统计
更新时间:2022年06月28日 09:08:43 作者:小旭2021
这篇文章介绍了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数据库字符串拼接、大小写转换以及substring详解
在日常工作中会遇到将多行的值拼接为一个值展现,下面这篇文章主要给大家介绍了关于PostgreSQL数据库字符串拼接、大小写转换以及substring的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下2023-04-04postgresql 如何查看pg_wal目录下xlog文件总大小
这篇文章主要介绍了postgresql 如何查看pg_wal目录下xlog文件总大小的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2021-01-01PostgreSQL 如何查找需要收集的vacuum 表信息
这篇文章主要介绍了PostgreSQL 如何查找需要收集的vacuum 表信息,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2021-02-02postgresql数据库 timescaledb 时序库 把大数据量表转换为超表的问题
这篇文章主要介绍了postgresql数据库 timescaledb 时序库 把大数据量表转换为超表,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2021-02-02
最新评论