例如,现在三月份,选择第一季度时一二月份求和
为什么3月份的不能求和???
----------------------------------以下是mysql测试的语句-------------------------
select t2.* from (
select t.month_s,t.year_code,t.number_s from (
select '1' as month_s,'2021' as year_code,38 as number_s union all
select '2' as month_s,'2021' as year_code,356 as number_s union all
select '3' as month_s,'2021' as year_code,493 as number_s union all
select '4' as month_s,'2021' as year_code,115 as number_s union all
select '5' as month_s,'2021' as year_code,565 as number_s union all
select '6' as month_s,'2021' as year_code,565 as number_s union all
select '7' as month_s,'2021' as year_code,341 as number_s union all
select '8' as month_s,'2021' as year_code,393 as number_s union all
select '9' as month_s,'2021' as year_code,582 as number_s union all
select '10' as month_s,'2021' as year_code,28 as number_s union all
select '11' as month_s,'2021' as year_code,689 as number_s union all
select '12' as month_s,'2021' as year_code,452 as number_s union all
select '1' as month_s,'2022' as year_code,396 as number_s union all
select '2' as month_s,'2022' as year_code,352 as number_s union all
select '3' as month_s,'2022' as year_code,331 as number_s union all
select '4' as month_s,'2022' as year_code,563 as number_s union all
select '5' as month_s,'2022' as year_code,160 as number_s
) t
where 1=1
/*取年*/
${if(len(year_code)=0,"","and t.year_code= '"+ year_code + "'")}
/*历史年的直接查询季度对应的数据,如果i不是历史年的数据,先按季度把每三个月的数据查询出来*/
${if(year_code<year(today())&& jd =1 ," and t.month_s in('1','2','3')",if(year_code<year(today())&& jd =2 ," and t.month_s in('4','5','6')",if(year_code<year(today())&& jd =3 ," and t.month_s in('7','8','9')",if(year_code<year(today())&& jd =4 ," and t.month_s in('10','11','12')",if(year_code=year(today()) && jd =1 ," and t.month_s in('1','2','3')",if(year_code=year(today()) && jd =2 ," and t.month_s in('4','5','6')",if(year_code=year(today()) && jd =3 ," and t.month_s in('7','8','9')"," and t.month_s in('10','11','12')")))))))}
) t2
where 1=1
and t2.month_s<'${month(today())}' /*最后再判断一次月份小于当前服务器的月份就行了*/
/*2022年1季度的sql限制*/