Time:09-18
CodePudding user response:
what result?
with tab1 as (Select 41 dept, 1 id, 1 nee from dual union all Select 42, 1 id, 2 nee from dual union all The select 43, 1 id, 3 nee from dual ) Tab2 as (The select t1. Id, t1. Dept, t1, nee, row_number () over (partition by t1. The id order by t1. Nee desc) rn from tab1 t1) The select t1. Id, Max (decode (t1. The rn, 1, t1. Dept, null)) new, Max (decode (t1. Rn, 2, t1. Dept, null)) old from tab2 t1 group by t1. Id ;
with TMP as (Select 4783 as dept_no, 362 as dept_name, 1 as the version from dual union all Select 4783, 362, 2 from dual union all Select 4771, 362, 3 from dual ) The select Max (decode (t2. Rn, 2, dept_no, null)) as "old project department," Max (decode (t2. Rn, 2, dept_name, null)) as "old project company," Max (decode (t2) rn, 1, dept_no, null)) as "new project department," Max (decode (t2) rn, 1, dept_name, null)) as "new project company" The from (select t1. *, row_number () over (partition by t1. Dept_name order by t1. Version desc) as an rn from TMP t1) t2 Where t2. Rn & lt;=2;
with TMP as (Select 4783 as dept_no, 362 as dept_name, 1 as the 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)) The from (select dept_no, dept_name, version, rownum rn from (select dept_no, dept_name, version from TMP order by version desc) Where rownum & lt;
Page link:https//www.codepudding.com/database/8147.html