oracle - SQL Join only the last historical row -
i'm sitting @ following problem: i'm writing view join several tables person table. , trying join partners table need historical last valid partner row:
partners table:
id, name, married_at, divorced_at, died_at, someone_id
as can see it's partners are/were married with. can have 1 partner @ time, several partners in history. last partner of (someone_id) may be:
- alive , still married
- alive divorced
- dead "but still married" (so widower)
i need find last partner row someone.
what got far:
select * someone_table s left join partners p on (p.someone_id = s.id , (p.divorced_at null , p.died_at null) )
but - obvious - gives me partners still alive , still married. sure these partners last partners of other "someones" whos last partner divorced or dead won't in result of statement. how other ones , 1 row each someone?
i tried select-statement table , using of rownum
select * s, (select * partners p p.someone_id = s.id , rownum = 1 order p.married_at)
but statement fails "invalied identifier s.id" error
note: table structure fixed , can't changed. dbms oracle.
thanks in advance
edit: sample data
partners_table
╔════╦═════════╦════════════╦═════════════╦════════════╦════════════╗ ║ id ║ name ║ married_at ║ divorced_at ║ died_at ║ someone_id ║ ╠════╬═════════╬════════════╬═════════════╬════════════╬════════════╣ ║ 1 ║ partner ║ 01.01.2000 ║ ║ ║ 12 ║ ║ 2 ║ honey1 ║ 15.01.2000 ║ 15.01.2001 ║ ║ 15 ║ ║ 3 ║ honey2 ║ 16.02.2001 ║ ║ ║ 15 ║ ║ 4 ║ beauty ║ 23.03.2005 ║ ║ 25.03.2005 ║ 16 ║ ║ 5 ║ lady1 ║ 11.11.2000 ║ 11.12.2000 ║ ║ 20 ║ ║ 6 ║ lady2 ║ 12.12.2000 ║ 01.01.2001 ║ ║ 20 ║ ║ 7 ║ lady3 ║ 02.02.2001 ║ ║ 04.02.2004 ║ 20 ║ ║ 8 ║ lady4 ║ 05.05.2005 ║ ║ ║ 20 ║ ║ 9 ║ mate ║ 23.06.2003 ║ 12.12.2009 ║ ║ 25 ║ ╚════╩═════════╩════════════╩═════════════╩════════════╩════════════╝
last historical rows be:
╔════╦═════════╦════════════╦═════════════╦════════════╦════════════╗ ║ id ║ name ║ married_at ║ divorced_at ║ died_at ║ someone_id ║ ╠════╬═════════╬════════════╬═════════════╬════════════╬════════════╣ ║ 1 ║ partner ║ 01.01.2000 ║ ║ ║ 12 ║ ║ 3 ║ honey2 ║ 16.02.2001 ║ ║ ║ 15 ║ ║ 4 ║ beauty ║ 23.03.2005 ║ ║ 25.03.2005 ║ 16 ║ ║ 8 ║ lady4 ║ 05.05.2005 ║ ║ ║ 20 ║ ║ 9 ║ mate ║ 23.06.2003 ║ 12.12.2009 ║ ║ 25 ║ ╚════╩═════════╩════════════╩═════════════╩════════════╩════════════╝
this ought want:
with partners (id, name, married_at, divorced_at, died_at, someone_id) (select 1, 'partner', to_date('01/01/2000', 'dd/mm/yyyy'), null, null, 12 dual union select 2, 'honey1', to_date('15/01/2000', 'dd/mm/yyyy'), to_date('15/01/2001', 'dd/mm/yyyy'), null, 15 dual union select 3, 'honey2', to_date('16/02/2001', 'dd/mm/yyyy'), null, null, 15 dual union select 4, 'beauty', to_date('23/03/2005', 'dd/mm/yyyy'), null, to_date('25/03/2005', 'dd/mm/yyyy'), 16 dual union select 5, 'lady1', to_date('11/11/2000', 'dd/mm/yyyy'), to_date('11/12/2000', 'dd/mm/yyyy'), null, 20 dual union select 6, 'lady2', to_date('12/12/2000', 'dd/mm/yyyy'), to_date('01/01/2001', 'dd/mm/yyyy'), null, 20 dual union select 7, 'lady3', to_date('02/02/2001', 'dd/mm/yyyy'), null, to_date('04/02/2004', 'dd/mm/yyyy'), 20 dual union select 8, 'lady4', to_date('05/05/2005', 'dd/mm/yyyy'), null, null, 20 dual union select 9, 'mate', to_date('23/06/2003', 'dd/mm/yyyy'), to_date('12/12/2009', 'dd/mm/yyyy'), null, 25 dual) select id, name, married_at, divorced_at, died_at, someone_id (select id, name, married_at, divorced_at, died_at, someone_id, row_number() on (partition someone_id order married_at desc) rn partners) rn = 1; id name married_at divorced_at died_at someone_id ---------- ------- ---------- ----------- ---------- ---------- 1 partner 01/01/2000 12 3 honey2 16/02/2001 15 4 beauty 23/03/2005 25/03/2005 16 8 lady4 05/05/2005 20 9 mate 23/06/2003 12/12/2009 25
Comments
Post a Comment