当前位置: 代码迷 >> Oracle管理 >> 取历次改变的情况的sql
  详细解决方案

取历次改变的情况的sql

热度:28   发布时间:2016-04-24 04:20:01.0
取每次改变的情况的sql
本帖最后由 hanklynn 于 2014-05-26 21:57:42 编辑



姓名 年度 工资
张三 2010 1500
张三 2011 1500
张三 2012 2000
张三 2013 2000
张三 2014 3000


姓名 变更年度 变更工资 原年度 原工资
张三 2014 3000 2013 2000
张三 2012 2000 2011 1500

------解决方案--------------------
select * from (
select b.name "姓名",b.year "变更年度",b.sal "变更工资",a.year "原年度",a.sal "原工资" from test_whq a
 right join test_whq b on a.name=b.name and a.year=b.year-1 and a.sal<>b.sal
) where "原年度" is not null order by "变更年度" desc 
------解决方案--------------------
终于明白楼主的意思了,下次加点描述吧

select a.姓名,
       a.年度 变更年度,
       a.工资 变更工资,
       b.年度 原年度,
       b.工资 原工资
  from t a, t b
 where a.姓名 = b.姓名
   and a.年度 > b.年度
   and a.工资 != b.工资
   and not exists (select 1
          from t
         where 姓名 = a.姓名
           and 年度 < a.年度
           and 年度 > b.年度)
order by 1,2 desc

和3楼的意思差不多
  相关解决方案