用sql怎么实现月累计数得到单月数

image.png

yzm265675 发布于 2021-12-7 09:19
1min目标场景问卷 立即参与
回答问题
悬赏:3 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共7回答
最佳回答
1
吾姓独孤Lv6高级互助
发布于2021-12-7 09:28(编辑于 2021-12-7 09:52)

用lag函数取上月数据 然后当月累计数据减去上月数据就是单月数据了

我这是oracle 你看你啥数据库,用你数据库的语法给lag做个为空置0处理

可能你数据库数字型减空时恒为空

SELECT

    ID

    , YEAR

    , MONTH

    , AMOUNT - NVL(LAG(AMOUNT) OVER(ORDER BY YEAR, MONTH),0) AS AMOUNT_MONTH

FROM

    TABLE

  • yzm265675 yzm265675(提问者) 大哥 这样写出来 表中第一个月的数据为空
    2021-12-07 09:41 
  • 吾姓独孤 吾姓独孤 回复 yzm265675(提问者) 不会啊,你是不是写反了,第一个月lag值为0 就是AMOUNT的数量
    2021-12-07 09:49 
  • 吾姓独孤 吾姓独孤 回复 yzm265675(提问者) 你啥数据库,是不是减空恒为空的,你给LAG做个空置0处理
    2021-12-07 09:51 
  • yzm265675 yzm265675(提问者) 回复 吾姓独孤 好了 ,刚刚没添加判断 mysql数据库 ifnull
    2021-12-07 09:59 
最佳回答
1
free_zzLv6中级互助
发布于2021-12-7 09:28

select

a.YEAR,

a.MONTH,

(b.AMOUNT-a.AMOUNT) AS 当月数

from table1 a left join table2 b on a.YEAR=b.YEAR and a.MONTH=b.MONTH-1

最佳回答
0
snrtuemcLv8专家互助
发布于2021-12-7 09:21

select year,month,sum(数量) as amount from 表 group by year,month

  • yzm265675 yzm265675(提问者) 大哥,这个表 是每月累计表,第二个月的数是第一个月加第二个月的,怎么用sql把他变成每个月的数而不是累计数
    2021-12-07 09:25 
  • snrtuemc snrtuemc 回复 yzm265675(提问者) 那就参考,http://blog.sina.com.cn/s/blog_13f91a7680102y0i2.html
    2021-12-07 09:26 
最佳回答
0
177741977Lv6初级互助
发布于2021-12-7 09:39
最佳回答
0
丶尛伟Lv6初级互助
发布于2021-12-7 09:43

SELECT

a.years,a.months,sum(c.amount) amount

from

(

select 2021 as years, 01 as months , 10 as amount from dual

union all

select 2021 as years, 02 as months , 34 as amount from dual

union all

select 2021 as years, 03 as months , 26 as amount from dual

union all

select 2021 as years, 04 as months , 54 as amount from dual

) a 

left join 

(

SELECT

years,months,sum(amount)  as amount

from

(

select 2021 as years, 01 as months , 10 as amount from dual

union all

select 2021 as years, 02 as months , 34 as amount from dual

union all

select 2021 as years, 03 as months , 26 as amount from dual

union all

select 2021 as years, 04 as months , 54 as amount from dual

) b

group by years,months

) c

on a.years = c.years and a.months >= c.months

GROUP BY a.years,a.months

我自己造了一些数据,写法是这样的

  • yzm265675 yzm265675(提问者) 你这是单月数变累计数么,还是通过累计数变单月数
    2021-12-07 09:47 
  • 丶尛伟 丶尛伟 回复 yzm265675(提问者) 单月变累计,你可以运行一下啊
    2021-12-07 09:50 
  • yzm265675 yzm265675(提问者) 回复 丶尛伟 这个我知道 ,但是累计变单月不会
    2021-12-07 09:53 
最佳回答
0
快乐星光Lv5中级互助
发布于2021-12-7 10:46

select a.YEAR,a.MONTH,

       case when a.MONTH = 1 then a.AMOUNT 

else a.AMOUNT - b.AMOUNT end as AMOUNT

from fr_hys_sumbyjidu_test a

left join

    (select YEAR,MONTH+1 as MONTH,AMOUNT from fr_hys_sumbyjidu_test) b on a.MONTH=b.MONTH

这个有一月份数据

最佳回答
0
罗罗诺亚索隆Lv5见习互助
发布于2021-12-7 10:50(编辑于 2021-12-7 11:02)

image.png

select 

t.month,

t.amount -t.amount2 

from (

select

t.month,

t.amount,

IF(t1.amount is null ,0,t1.amount)amount2 

from 

(

SELECT

id month,

SUM(cnt) amount

FROM

exam_test

WHERE

year = '2020' 

GROUP BY

month

)t

LEFT JOIN

(

SELECT

id+1 month,

SUM(cnt) amount

FROM

exam_test

WHERE

year = '2020' 

and id<='11'

GROUP BY

month

)t1

on t.month =t1.month

)t

image.png

  • 8关注人数
  • 702浏览人数
  • 最后回答于:2021-12-7 11:02
    请选择关闭问题的原因
    确定 取消
    返回顶部