如何在postgresql中获取每月数据的列表日期 [英] How to get list day of month data per month in postgresql
本文介绍了如何在postgresql中获取每月数据的列表日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 psql v.10.5
i use psql v.10.5
我有一个这样的结构表:
and i have a structure table like this :
| date | total |
-------------------------
| 01-01-2018 | 50 |
| 05-01-2018 | 90 |
| 30-01-2018 | 20 |
如何按月获取回顾数据,但数据显示连续 30 天,我希望数据显示如下:
how to get recap data by month, but the data showed straight 30 days, i want the data showed like this :
| date | total |
-------------------------
| 01-01-2018 | 50 |
| 02-01-2018 | 0 |
| 03-01-2018 | 0 |
| 04-01-2018 | 0 |
| 05-01-2018 | 90 |
.....
| 29-01-2018 | 0 |
| 30-01-2018 | 20 |
我试过这个查询:
SELECT * FROM date
WHERE EXTRACT(month FROM "date") = 1 // dynamically
AND EXTRACT(year FROM "date") = 2018 // dynamically
但结果不是我所期望的.还有我动态创建的月份和日期参数.
but the result is not what i expected. also the params of month and date i create dynamically.
任何帮助将不胜感激
推荐答案
使用函数 generate_series(start, stop, step interval)
,例如:
Use the function generate_series(start, stop, step interval)
, e.g.:
select d::date, coalesce(total, 0) as total
from generate_series('2018-01-01', '2018-01-31', '1 day'::interval) d
left join my_table t on d::date = t.date
这篇关于如何在postgresql中获取每月数据的列表日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文