请求中缺少添加日期

5cg8jx4n  于 2021-06-10  发布在  Cassandra
关注(0)|答案(1)|浏览(262)

例如,我想在请求中列出两个日期之间缺少的日期
我的数据:
表顺序

DATE_order  | AMOUNT    
01/01/2020  |  500    
01/01/2020  |  600    
03/01/2020  |  100    
05/01/2020  |  300

我要把请求退回

01/01/2020   | 1100    
02/01/2020   | 0    
03/01/2020   | 100    
04/01/2020   | 0    
05/01/2020   | 300

我使用带有apach hive连接器的cassandra数据库
有人能帮我吗?

qacovj5a

qacovj5a1#

可以使用“侧视图”和“分解”生成缺少的行:

with your_data as (
select stack(4,
'2020-01-01',500,    
'2020-01-01',600,   
'2020-01-03',100,    
'2020-01-05',300
) as (DATE_order,AMOUNT )
)

select date_sub(s.date_order ,nvl(d.i,0)) as date_order, case when d.i > 0 then 0 else s.amount end as amount
from
(--find previous date
select date_order, amount, 
        lag(date_order) over(order by date_order) prev_date,
        datediff(date_order,lag(date_order) over(order by date_order)) datdiff
from
( --aggregate
 select date_order, sum(amount) amount from your_data group by date_order )s
)s
--generate rows
lateral view outer posexplode(split(space(s.datdiff-1),' ')) d as i,x
order by date_order;

结果:

date_order      amount
2020-01-01      1100
2020-01-02      0
2020-01-03      100
2020-01-04      0
2020-01-05      300
Time taken: 10.04 seconds, Fetched: 5 row(s)

相关问题