我有一个db表,结构如下
id | dateCreated | numOfUsers
其中典型的行是1,'2011-10-13 12:00:00',4
我的行包含过去4个月的数据,但是有相当多的天丢失了,我想找出使用SQL丢失的天,有什么想法我如何写这个查询?
我怀疑你是通过某种方式得到一个日期列表并将其与数据库中的值进行比较来实现这一点的。
我知道您可以使用php或其他编程语言使用多个查询来完成这项工作,但是如果可能的话,我希望在数据库级别完成这项工作。
提前谢谢
最佳答案
对于PostgreSQL,使用generate_series()函数可以很容易地动态生成“日期列表”:
with all_dates as (
select cast((current_date - interval '4' month) as date) + i as the_date
from generate_series(0, extract(day from current_date - (current_date - interval '4' month))::int) as i
)
select ad.the_date,
y.id,
y.numOfUsers
from all_dates t
left join your_table y ON y.dateCreated = t.the_date;