求一语句,在线,谢谢了!!!~~~~~~~~~~~~~~~~~~~~~~~~~~@
数据库中有num1,num2,year,month,sum字段
想求一复合语句:
.... where year=当前年 and month=当前月-1
如果当前是1月份的话:
... where year=当前年-1 and month=‘12’
<这我已经实现了,最关键的是想还把在这个条件的基础上的月份-1的sum值也一起显示出来,最后的字段应该为:
select num1,num2,year,month,sum,sum(当前月-2的值相当于) where ...
非常感谢!
------解决方案--------------------select * from
(select num1,num2,year,month,sum,
lag(num1, 1, 0) over(order by year||month ) as pre_num1,
lag(num2, 1, 0) over(order by year||month ) as pre_num2,
lag(year, 1, 0) over(order by year||month ) as pre_year,
lag(month, 1, 0) over(order by year||month ) as pre_month,
lag(sum, 1, 0) over(order by year||month ) as pre_sum
from table_name
where year || month > = to_char(add_months(sysdate,-2), 'yyyymm ')
and year || month <= to_char(add_months(sysdate,-1), 'yyyymm ')
)
where year || month = to_char(add_months(sysdate,-1), 'yyyymm ')
where year = to_char(add_months(sysdate,-1), 'yyyy ') and month = to_char(add_months(sysdate,-1), 'mm ')