gpt4 book ai didi

sql - 填补查询返回值的空白

转载 作者:行者123 更新时间:2023-12-04 17:56:25 26 4
gpt4 key购买 nike

我有一个包含如下列的数据库。

Id    Date              Value
1 12/01/2010 09:30 127.31
1 12/01/2010 09:31 133.41
1 12/01/2010 09:32 147.54
1 12/01/2010 09:34 155.66

本质上,我存储与时间戳对应的值。但是,如果值为 0,我不存储它(它是一个非常大的数据库,并且 0 经常出现,所以我们决定不包括这些行以节省空间)。在上面的例子中, 12/01/2010 09:33有一个 0 值所以它不存储。但是,当用户查询数据库时,他会执行类似的操作
select * from table where Id = '1' and Date > to_date('12/01/2010', 'MM/DD/YYYY')`

我需要填补空白并使用 0 值提及时间戳。我怎样才能做到这一点?

最佳答案

select 
nvl(b.id,1) as id,
alldates.Date as Date,
nvl(b.value,0) as value
from
(select level/1440 + to_date('12/01/2010', 'MM/DD/YYYY') as Date
from dual
connect by level < 1440 --one day
) alldates
left join
(select * from table where Id = '1' and Date > to_date('12/01/2010', 'MM/DD/YYYY') b
on alldates.Date = b.Date

更新 (回复评论):
select 
nvl(b.id,1) as id,
alldates.Date as Date,
nvl(b.value,0) as value,
nvl(b.value, lag(b.value) over (order by b.Date nulls last)) last_valid_value
from
(select level/1440 + to_date('12/01/2010', 'MM/DD/YYYY') as Date
from dual
connect by level < 1440 --one day
) alldates
left join
(select * from table where Id = '1' and Date > to_date('12/01/2010', 'MM/DD/YYYY') b
on alldates.Date = b.Date

关于sql - 填补查询返回值的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8253612/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com