日月年趋势的sql

日趋势(从昨天开始往前30天):

select  sum(data_d)  from  A  where code =#{code}  

and date_d  between to date(to_char(sysdate-31,'yyyy-mm-dd'),'yyyy-mm-dd')

and  to date(to_char(sysdate-1,'yyyy-mm-dd'),'yyyy-mm-dd')

月趋势(这个月就取昨天的数据,其他月取之前十一个月的最后一天)

select  sum(data_m)  from  A  where code =#{code}  

and 

(

    (date_m <=(last_day(add_months(trunc(sysdate),-1)))  and

    substr(to_char(date_m,'yyyy-mm-dd'),6,10)=substr(to_char(last_day(trunc(date_m)),'yyyy-mm-dd'),6,10) and

    date_m >=last_day(add_months(trunc(sysdate),-12))) or date_m=trunc(sysdate-1)

(

年趋势(今年的就取昨天的年值,其他的取每年的最后一天的年值)

select sum(data_y) from A where code =#{code}

and date_y=trunc(sysdate-1)

union all

select sum(data_y) from A where code =#{code}

and date_y <=trunc(sysdate,'yyyy')  and substr(to_char(date_y,'yyyy-mm-dd'),5,8)='1231'

猜你喜欢

转载自blog.csdn.net/weixin_41126842/article/details/83538053