数据累计语句

我想实现字段根据选择的日期自动查出月累积量(从查询日期当月1号到查询日期)年累计量 (当年第一天到查询日期累积量)怎么写sql啊求大佬帮帮忙

FineReport iHYelRsB 发布于 2023-9-18 17:29
1min目标场景问卷 立即参与
回答问题
悬赏:3 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共2回答
最佳回答
0
free_zzLv6中级互助
发布于2023-9-18 17:41(编辑于 2023-9-18 17:45)

select

     substr(a.date,1,7)  as date_month -- yyyy-MM

    ,sum(b.数据) as data_num     -- 月累计

from table1 a 

left join table1 b  on a.date >= b.date

where substr(a.date,1,7) = substr('${date_time}',1,7) and a.date <= '${date_time}'

group by substr(a.date,1,7) 

--

年累计改下where后面的条件就好了

select

     substr(a.date,1,4)  as date_month -- yyyy

    ,sum(b.数据) as data_num     -- 年累计

from table1 a 

left join table1 b  on a.date >= b.date

where substr(a.date,1,7) = substr('${date_time}',1,4) and a.date <= '${date_time}'

group by substr(a.date,1,4) 

最佳回答
0
Z4u3z1Lv6专家互助
发布于2023-9-18 17:30
  • 2关注人数
  • 339浏览人数
  • 最后回答于:2023-9-18 17:45
    请选择关闭问题的原因
    确定 取消
    返回顶部