一个SQL问题

请教一下,数据库中以年月(202107)存储的每个月的销售目标,按控件筛选的起始终止日期提取这段时间内的目标SQL应该怎么写?

比如20210208~20210712,2月/28*21+3月+4月+5月+6月+7月/31*12

或者数据怎么存比较好呢?

吴三思 发布于 2021-7-28 14:24
1min目标场景问卷 立即参与
回答问题
悬赏:3 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共2回答
最佳回答
0
杨朝健Lv5中级互助
发布于2021-7-28 15:44(编辑于 2021-7-28 15:56)

$date1=20210208   $date2=20210712

ds1:select month,target from table where month between '${date1}' and '${date2}' order by month

目标汇总单元格=

sum(maparray(ds1.select(target),

if(index=1,

item/DAYSOFMONTH(todate($date1,"yyyyMMdd"))*(DAYSOFMONTH(todate($date1,"yyyyMMdd"))-right($date1,2)+1),

if(index=count(ds1.select(target)),item/DAYSOFMONTH(todate($date2,"yyyyMMdd"))*right($date2,2)

,item))

))

或者直接在SQL里处理

select month,

case when month='${left(date1,4)}' then target/${DAYSOFMONTH(todate(date1,"yyyyMMdd"))}*${DAYSOFMONTH(todate(date1,"yyyyMMdd"))-right(date1,2)+1}

     when month='${left(date2,4)}' then target/${DAYSOFMONTH(todate(date2,"yyyyMMdd"))}*${right(date2,2)-0}

     else target end as 目标

from table

where month between '${date1}' and '${date2}'

order by month

最佳回答
0
free_zzLv6中级互助
发布于2021-7-28 15:13(编辑于 2021-7-28 15:31)

你数据库是存的年月,那你用控件筛选就筛选这个时间范围啊,不知道你的问题在哪

where

concat(substring(日期字段,1,4),"-",substring(日期字段,5,2))> ${left(起始日期,7)}

and concat(substring(日期字段,1,4),"-",substring(日期字段,5,2))< ${left(结束日期,7)}

  • 吴三思 吴三思(提问者) 我的意思是筛选的不是整月,要计算筛选时间段内的目标,不是整月的要根据月度目标除以月度天数乘以需要的天数
    2021-07-28 15:36 
  • 3关注人数
  • 329浏览人数
  • 最后回答于:2021-7-28 15:56
    请选择关闭问题的原因
    确定 取消
    返回顶部