补齐用户账户每天的金额记录

1 篇文章 0 订阅
1 篇文章 0 订阅
文章展示了如何使用SQL查询从用户账户余额表test_data和日期维度表test_dim_date中获取每日余额,通过Lead函数处理数据,填充缺失日期的记录,确保每个用户在每一天都有对应的账户金额。
摘要由CSDN通过智能技术生成

需求:

有用户账户余额表 test_data ,具体数据如下

create table test_data as 
select 'u1' as user_id,'2023-01-01' as rdate,100 as xmoney union all 
select 'u1' as user_id,'2023-02-01' as rdate,150 as xmoney union all 
select 'u1' as user_id,'2023-02-10' as rdate,130 as xmoney union all 
select 'u2' as user_id,'2023-01-01' as rdate,110 as xmoney union all 
select 'u2' as user_id,'2023-01-11' as rdate,140 as xmoney union all 
select 'u2' as user_id,'2023-02-01' as rdate,180 as xmoney ;

假设当前日期为2023年2月15日,给出每天用户的账户金额(补齐缺失日期的记录)

create table test_dim_date as 
select '2023-01-01' as xdate union all 
select '2023-01-02' as xdate union all 
select '2023-01-03' as xdate union all 
select '2023-01-04' as xdate union all 
select '2023-01-05' as xdate union all 
select '2023-01-06' as xdate union all 
select '2023-01-07' as xdate union all 
select '2023-01-08' as xdate union all 
select '2023-01-09' as xdate union all 
select '2023-01-10' as xdate union all 
select '2023-01-11' as xdate union all 
select '2023-01-12' as xdate union all 
select '2023-01-13' as xdate union all 
select '2023-01-14' as xdate union all 
select '2023-01-15' as xdate union all 
select '2023-01-16' as xdate union all 
select '2023-01-17' as xdate union all 
select '2023-01-18' as xdate union all 
select '2023-01-19' as xdate union all 
select '2023-01-20' as xdate union all 
select '2023-01-21' as xdate union all 
select '2023-01-22' as xdate union all 
select '2023-01-23' as xdate union all 
select '2023-01-24' as xdate union all 
select '2023-01-25' as xdate union all 
select '2023-01-26' as xdate union all 
select '2023-01-27' as xdate union all 
select '2023-01-28' as xdate union all 
select '2023-01-29' as xdate union all 
select '2023-01-30' as xdate union all 
select '2023-01-31' as xdate union all 
select '2023-02-01' as xdate union all 
select '2023-02-02' as xdate union all 
select '2023-02-03' as xdate union all 
select '2023-02-04' as xdate union all 
select '2023-02-05' as xdate union all 
select '2023-02-06' as xdate union all 
select '2023-02-07' as xdate union all 
select '2023-02-08' as xdate union all 
select '2023-02-09' as xdate union all 
select '2023-02-10' as xdate union all 
select '2023-02-11' as xdate union all 
select '2023-02-12' as xdate union all 
select '2023-02-13' as xdate union all 
select '2023-02-14' as xdate union all 
select '2023-02-15' as xdate ;

--  常规写法(正确)

select 
    ta.xdate
   ,tb.rdate
   ,tb.user_id
   ,tb.xmoney
from test_dim_date ta,
(select * ,lead(rdate,1,'2099-12-31') over(partition by user_id order by rdate) as lastday from test_data) tb 
where ta.xdate>=tb.rdate 
and ta.xdate < tb.lastday
order by user_id,xdate 

-- spark-sql 写法

select 
     xdate
    ,user_id
    ,xmoney
    ,last_value(xmoney) ignore nulls over (partition by user_id order by xdate) as last_value_xmoney  
from 
(
    select 
        ta.xdate,tb.user_id,case when ta.xdate = tb.rdate then tb.xmoney end as xmoney 
    from test_dim_date ta,
    (select * ,lead(rdate,1,'2099-12-31') over(partition by user_id order by rdate) as lastday from test_data) tb 
    where ta.xdate>=tb.rdate 
    and ta.xdate < tb.lastday
) tt 
order by user_id,xdate 

-- hive-sql写法   

select 
     xdate
    ,user_id
    ,xmoney
    ,last_value(xmoney,true) over (partition by user_id order by xdate) as last_value_xmoney 
from 
(
    select 
        ta.xdate,tb.user_id,case when ta.xdate = tb.rdate then tb.xmoney end as xmoney 
    from test_dim_date ta,
    (select * ,lead(rdate,1,'2099-12-31') over(partition by user_id order by rdate) as lastday from test_data) tb 
    where ta.xdate>=tb.rdate 
    and ta.xdate < tb.lastday
) tt 
order by user_id,xdate 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值