Postgresql按时间分组统计查询(年月日周时分秒)
create table public."user" (
id integer primary key not null,
create_time bigint,
update_time bigint,
delete_time bigint,
created_at TIMESTAMP,
updated_at TIMESTAMP,
deleted_at TIMESTAMP
);
年
select to_char(to_timestamp(create_time / 1000), 'YYYY') as year, COUNT(*) as num
from "user"
where to_char(to_timestamp(create_time / 1000), 'YYYY-MM-DD') >= '2023-01-01'
and to_char(to_timestamp(create_time / 1000), 'YYYY-MM-DD') <= '2024-01-01'
group by year
order by year;
select date_part('year', to_timestamp(create_time / 1000)) as year, COUNT(*) as num
from "user"
where to_char(to_timestamp(create_time / 1000), 'YYYY-MM-DD') >= '2023-01-01'
and to_char(to_timestamp(create_time / 1000), 'YYYY-MM-DD') <= '2024-01-01'
group by year
order by year;
select to_char(created_at::DATE, 'YYYY') as year, COUNT(*) as num
from "user"
where created_at >= '2023-01-01'
and created_at <= '2024-01-01'
group by year
order by year;
月
select to_char(to_timestamp(create_time / 1000), 'YYYY-MM') as month, COUNT(*) as num
from "user"
where to_char(to_timestamp(create_time / 1000), 'YYYY-MM-DD') >= '2023-01-01'
and to_char(to_timestamp(create_time / 1000), 'YYYY-MM-DD') <= '2024-01-01'
group by month
order by month;
select to_char(created_at::DATE, 'YYYY-MM') as month, COUNT(*) as num
from "user"
where created_at >= '2023-01-01'
and created_at <= '2024-01-01'
group by month
order by month;
周
select to_char(to_timestamp(create_time / 1000) - (extract(dow from to_timestamp(create_time / 1000))-1 || 'day')::interval, 'YYYY-mm-dd') as week, COUNT(*) as num
from "user"
where to_char(to_timestamp(create_time / 1000), 'yyyy-mm-dd') >= '2023-01-01'
and to_char(to_timestamp(create_time / 1000), 'yyyy-mm-dd') <= '2024-01-01'
group by week
order by week;
select to_char(created_at::DATE - (extract(dow from created_at::TIMESTAMP)-1 || 'day')::interval, 'YYYY-mm-dd') week, COUNT(*) as num
from "user"
where created_at >= '2023-01-01'
and created_at <= '2024-01-01'
group by week
order by week;
日
select to_char(to_timestamp(create_time / 1000), 'YYYY-MM-DD') as day, COUNT(*) as num
from "user"
where to_char(to_timestamp(create_time / 1000), 'YYYY-MM-DD') >= '2023-01-01'
and to_char(to_timestamp(create_time / 1000), 'YYYY-MM-DD') <= '2024-01-01'
group by day
order by day;
select to_char(created_at::DATE, 'YYYY-MM-DD') as day, COUNT(*) as num
from "user"
where created_at >= '2023-01-01'
and created_at <= '2024-01-01'
group by day
order by day;
小时
select to_char(to_timestamp(create_time / 1000), 'YYYY-MM-DD HH24') as hour, COUNT(*) as num
from "user"
where to_char(to_timestamp(create_time / 1000), 'YYYY-MM-DD') >= '2023-01-01'
and to_char(to_timestamp(create_time / 1000), 'YYYY-MM-DD') <= '2024-01-01'
group by hour
order by hour;
select to_char(created_at::DATE, 'YYYY-MM-DD HH24') as hour, COUNT(*) as num
from "user"
where created_at >= '2023-01-01'
and created_at <= '2024-01-01'
group by hour
order by hour;
分钟
select to_char(to_timestamp(create_time / 1000), 'YYYY-MM-DD HH24:MI') as minute, COUNT(*) as num
from "user"
where to_char(to_timestamp(create_time / 1000), 'YYYY-MM-DD') >= '2023-01-01'
and to_char(to_timestamp(create_time / 1000), 'YYYY-MM-DD') <= '2024-01-01'
group by minute
order by minute;
select to_char(created_at::DATE, 'YYYY-MM-DD HH24:MI') as minute, COUNT(*) as num
from "user"
where created_at >= '2023-01-01'
and created_at <= '2024-01-01'
group by minute
order by minute;
秒
select to_char(to_timestamp(create_time / 1000), 'YYYY-MM-DD HH24:MI:SS') as second, COUNT(*) as num
from "user"
where to_char(to_timestamp(create_time / 1000), 'YYYY-MM-DD') >= '2023-01-01'
and to_char(to_timestamp(create_time / 1000), 'YYYY-MM-DD') <= '2024-01-01'
group by second
order by second;
select to_char(created_at::DATE, 'YYYY-MM-DD HH24:MI:SS') as second, COUNT(*) as num
from "user"
where created_at >= '2023-01-01'
and created_at <= '2024-01-01'
group by second
order by second;
参考资料