PostgreSQL 實現按月按年,按日統計 分組統計
參考部落格園的文章,暫時在這裡整理一下,後期再做補充。
--按年分組檢視
select to_char(time_field, 'YYYY') as d , count(id) as total_count,sum (count_field) as total_amount from table_namewhere time_field between start_time and end_time group by d
--按月分組檢視
select to_char(time_field, 'YYYY-MM') as d , count(id) as total_count,sum (count_field) as total_amount from table_name
where time_field between start_time and end_time group by d
--按天分組檢視
select to_char(time_field, 'YYYY-MM-DD') as d , count(id) as total_count,sum (count_field) as total_amount from table_name
where time_field between start_time and end_time group by d
--按小時分組檢視
select to_char(time_field, 'YYYY-MM-DD HH24 ' ) as d , count(id) as total_count,sum (count_field) as total_amount from table_name
where time_field between start_time and end_time group by d order by d
--按秒分組檢視
select to_char(time_field, 'YYYY-MM-DD HH24:MI:SS ' ) as d , count(id) as total_countl,sum (count_field) as total_amount from table_name
where time_field between start_time and end_time group by d