表a:
字段: month salary
要求:查询每个月的该月月份、工资、上一个月工资、下一个月工资
显示格式:month salary lastsalary nextsalary
------解决方案--------------------
lead,lag()函数实现
------解决方案--------------------
- SQL code
select month,salary,lag(salary,1,null) lastsalary,lead(salary,1,null)nextsalary from a
------解决方案--------------------
如果表中月份是按顺序排列的话 上面可以了 不是的话 可以先排序再获取 或者使用add_mongths()来计算获取
------解决方案--------------------
------解决方案--------------------
- SQL code
SELECT MONTH,SALARY,LEAD(SALARY,1,SALARY)OVER(ORDER BY MONTH) LASTSALARY, LAG(SALARY,1,SALARY)OVER(ORDER BY MONTH) NEXTSALARY FROM( SELECT MONTH,SUM(SALARY) SALARY FROM T GROUP BY MONTH ) T1;
------解决方案--------------------
with a
as
(
select 2006 salary,6 month from dual
union select 2007 salary,7 month from dual
union select 2008 salary,8 month from dual
)
select month,salary,
lag(salary,1,null) over(order by month) lastsalary,
lead(salary,1,null) over(order by month) nextsalary
from a