计算截止每个时间点的累计值

该博客介绍如何在Hive中使用SQL查询计算截止到每个月底的公司累计金额。通过时间补全和窗口函数over(),对每个公司的月度交易进行分组并按时间顺序累计总额。
摘要由CSDN通过智能技术生成
-- 计算每个公司每个月的累计值(这种情况下,如果某个公司某个月没有销售额,这个公司在这个月的记录就没有)
select t.year,
    t.month,
    companyid,
    companyname,
    sum(amount) over(partition by companyid, companyname,year order by t.month) amount
from (
    select t.year,
        t.month,
        companyid,
        companyname,
        sum(amount) amount
    from property_finance_final
    group by t.year, t.month, companyid, companyname

) t


--时间补全的截至每个月月底累计值

select substr(t1.the_month,1,4),substr(t1.the_month,6,2),
    t2.companyid,t2.companyname,sum(t2.amount) amount
from(
    select the_month
    from dim.dim_date
    where the_date<'2018-07-02'
    group by the_month
)t1
left join
(
    select t.year,
        t.month,
        companyid,
        companyname,
        sum(amount) over(partition by company

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值