17,086
社区成员
发帖
与我相关
我的任务
分享
with tmp as (
select 4783 as dept_no, 362 as dept_name, 1 as version from dual union all
select 4783, 362, 2 from dual union all
select 4771, 362, 3 from dual
)
select
max(decode(rn,2,dept_no,null)) ,max(decode(rn,2,dept_name,null)),
max(decode(rn,1,dept_no,null)) ,max(decode(rn,1,dept_name,null))
from
(select dept_no,dept_name,version,rownum rn from (select dept_no,dept_name,version from tmp order by version desc)
where rownum <=2 )
with tmp as (
select 4783 as dept_no, 362 as dept_name, 1 as version from dual union all
select 4783, 362, 2 from dual union all
select 4771, 362, 3 from dual
)
select max(decode(t2.rn,2,dept_no,null)) as "旧项目部门",
max(decode(t2.rn,2,dept_name,null)) as "旧项目公司",
max(decode(t2.rn,1,dept_no,null)) as "新项目部门",
max(decode(t2.rn,1,dept_name,null)) as "新项目公司"
from (select t1.*, row_number() over(partition by t1.dept_name order by t1.version desc) as rn from tmp t1) t2
where t2.rn <= 2;
with tab1 as (
select 41 dept, 1 id, 1 nee from dual union all
select 42, 1 id, 2 nee from dual union all
select 43, 1 id, 3 nee from dual
)
,tab2 as (
select t1.id, t1.dept, t1.nee, row_number() over(partition by t1.id order by t1.nee desc) rn from tab1 t1 )
select t1.id, max(decode(t1.rn, 1, t1.dept, null)) new, max(decode(t1.rn, 2, t1.dept, null)) old from tab2 t1 group by t1.id
;