SQL仅加入最后一个历史行

时间:2015-05-12 10:28:26

标签: sql oracle greatest-n-per-group

我正处理以下问题:我正在编写一个视图,其中我将多个表连接到人员表。我现在尝试加入合作伙伴表,但我只需要历史上最后一个有效的合作伙伴行:

合作伙伴表:

id,
name,
married_at,
divorced_at,
died_at,
someone_id

正如您所看到的那样,您与已婚/已婚的伴侣有关。有人可以一次只有一个合作伙伴,但历史上有几个合作伙伴。所以某人(someone_id)的最后一个伙伴可能是:

  • 活着但仍然结婚
  • 活着但离婚
  • 死了"但仍然结婚" (所以有人是w夫)

我只需找到某人的最后一个合作伙伴行。

到目前为止我得到了什么:

select *
from someone_table s
left join partners p on (p.someone_id = s.id and (p.divorced_at is null and p.died_at is null) )

但是这个 - 显而易见的 - 让我只有仍然活着但仍然结婚的伴侣。当然,这些合作伙伴是某人的最后合作伙伴,但所有其他合作伙伴"某些人"最后一个伴侣离婚或死亡的人不会参与声明的结果。我如何获得其他人,每个人只有一行?

我还尝试将select语句作为表并使用rownum

select *
from someone s,
(select * from partners p where p.someone_id = s.id and ROWNUM = 1 order by p.married_at)

但是这个陈述总是失败,并且有一个"无效的标识符s.id"错误

注意:表格结构是固定的,无法更改。 DBMS是oracle。

提前致谢

编辑: 样本数据

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 ║
╚════╩═════════╩════════════╩═════════════╩════════════╩════════════╝

最后的历史行将是:

╔════╦═════════╦════════════╦═════════════╦════════════╦════════════╗
║ 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 ║
╚════╩═════════╩════════════╩═════════════╩════════════╩════════════╝

4 个答案:

答案 0 :(得分:3)

这应该做你想做的事:

with partners (id, name, married_at, divorced_at, died_at, someone_id) as (select 1, 'partner', to_date('01/01/2000', 'dd/mm/yyyy'), null, null, 12 from dual union all
                                                                           select 2, 'honey1', to_date('15/01/2000', 'dd/mm/yyyy'), to_date('15/01/2001', 'dd/mm/yyyy'), null, 15 from dual union all
                                                                           select 3, 'honey2', to_date('16/02/2001', 'dd/mm/yyyy'), null, null, 15 from dual union all
                                                                           select 4, 'beauty', to_date('23/03/2005', 'dd/mm/yyyy'), null, to_date('25/03/2005', 'dd/mm/yyyy'), 16 from dual union all
                                                                           select 5, 'lady1', to_date('11/11/2000', 'dd/mm/yyyy'), to_date('11/12/2000', 'dd/mm/yyyy'), null, 20 from dual union all
                                                                           select 6, 'lady2', to_date('12/12/2000', 'dd/mm/yyyy'), to_date('01/01/2001', 'dd/mm/yyyy'), null, 20 from dual union all
                                                                           select 7, 'lady3', to_date('02/02/2001', 'dd/mm/yyyy'), null, to_date('04/02/2004', 'dd/mm/yyyy'), 20 from dual union all
                                                                           select 8, 'lady4', to_date('05/05/2005', 'dd/mm/yyyy'), null, null, 20 from dual union all
                                                                           select 9, 'mate', to_date('23/06/2003', 'dd/mm/yyyy'), to_date('12/12/2009', 'dd/mm/yyyy'), null, 25 from dual)
select id,
       name,
       married_at,
       divorced_at,
       died_at,
       someone_id
from   (select id,
               name,
               married_at,
               divorced_at,
               died_at,
               someone_id,
               row_number() over (partition by someone_id order by married_at desc) rn
        from   partners)
where  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

答案 1 :(得分:1)

如果我理解你的问题(我相信我的话),你应该尝试这样的事情:

SELECT *
FROM someone_table s
left join (
    SELECT *
    FROM (
        SELECT *
        FROM partners p
        WHERE p.someone_id = s.id
        ORDER BY GREATEST(died_at, divorced_at, married_at)
    ) x
    WHERE ROWNUM = 1 
 ) y

注意:我不是一个oracle人,我的大部分工作都是使用sql server,但是according to this post greatest应该在oracle数据库上工作。

答案 2 :(得分:1)

方法1

SELECT 
    * 
FROM 
    partners 
WHERE 
    someone_id = $someone_id 
AND 
    married_at = (SELECT MAX(married_at) FROM partners WHERE someone_id = $someone_id GROUP BY someone_id);

方法2

SELECT 
    p.*
FROM
    partners p
INNER JOIN
(
    SELECT 
        someone_id, MAX(married_at) as lastmarried_at
    FROM 
        partners
    GROUP BY
        someone_id
) m
ON m.someone_id = p.someone_id AND m.lastmarried_at = p.married_at
where p.someone_id in ($someone_id1, $someone_id2);

注意:将$ someone_id替换为实际值

答案 3 :(得分:0)

SELECT
    ID,
    Name,
    MAX(Married_At) LastMarriedAt,
    MAX(Divorced_At) KEEP (DENSE_RANK LAST ORDER BY Married_At) LastDivorcedAt,
    MAX(Died_At) KEEP (DENSE_RANK LAST ORDER BY Married_At) LastDiedAt,
    MAX(Someone_ID) KEEP (DENSE_RANK LAST ORDER BY Married_At) LastSomeoneID
FROM
    Partners
GROUP BY
    ID,
    Name

参见示例:http://sqlfiddle.com/#!4/3c073/1

编辑:根据样本数据,需要移动一些列

SELECT
    Someone_ID,
    MAX(Name) KEEP (DENSE_RANK LAST ORDER BY Married_At) Name,
    MAX(Married_At) LastMarriedAt,
    MAX(Divorced_At) KEEP (DENSE_RANK LAST ORDER BY Married_At) LastDivorcedAt,
    MAX(Died_At) KEEP (DENSE_RANK LAST ORDER BY Married_At) LastDiedAt,
    MAX(ID) KEEP (DENSE_RANK LAST ORDER BY Married_At) LastID
FROM
    Partners
GROUP BY
    Someone_ID