【问题标题】:Join Multiple tables and get only the latest data (Oracle)加入多个表并仅获取最新数据(Oracle)
【发布时间】:2020-11-14 10:41:38
【问题描述】:

Table 1

emp_id  firstName   dept_code   upd_date
------------------------------------------------
100     John        lti         28-05-2020
100     John        r&d         22-05-2020
100     John        sap         20-05-2020
101     David       sap         25-07-2020
101     David       lti         22-07-2020
101     David       r&d         20-07-2020
103     Mac         der         24-08-2020
103     Mac         fbi         22-08-2020
103     Mac         sap         21-08-2020
103     Mac         lot         20-08-2020
104     Sean        fbi         29-09-2020
104     Sean        lti         23-09-2020
104     Sean        sap         22-09-2020

Table 2

emp_id  bu_code bu_location upd_date
-------------------------------------------
100     455     cypris      22-07-2020
100     667     cambodia    20-07-2020
101     788     argentina   29-09-2020
101     998     egypt       22-09-2020
103     454     russia      29-07-2020
103     123     germany     22-07-2020
104     344     india       25-01-2020
104     556     nepal       24-01-2020
104     778     new Zealand 23-01-2020
104     990     canada      22-01-2020

Table 3

emp_id  street      pinCode upd_date
------------------------------------------
100     baker       411057  30-04-2019
100     el camino   311098  22-04-2019
100     redmond     344566  23-03-2019
100     harbour     232345  22-03-2019
101     standford   122334  26-02-2019
101     wellington  567890  22-02-2019
103     rosemund    333444  31-03-2019
103     creek       656776  27-02-2019
103     river       432234  25-02-2019
103     ontario     987789  23-02-2019
103     faux        345555  22-02-2019
104     bluebus     112211  24-02-2019
104     gambel      344898  22-02-2019

Resultset

emp_id  firstName   dept_code   bu_code bu_location street      pinCode
-------------------------------------------------------------------------
100     John        lti         455     cypris      baker       411057
101     David       sap         788     argentina   standford   122334
103     Mac         der         454     russia      rosemund    333444
104     Sean        fbi         344     india       bluebus     112211

大家好,

我有一个复杂的要求(至少对我来说),我们需要根据 emp_id 连接来自多个表的数据,但另一个约束是我们只需要显示每个表中特定员工的最新数据。前三张图片显示了三个带有样本数据的表,第四张图片显示了我们对查询的期望。如果有人可以提供解决方案或至少指导我找到解决方案,那将非常有帮助。

谢谢

【问题讨论】:

  • 样本数据最好显示为formatted text。请参阅here 了解有关如何创建漂亮表格的一些提示。

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


【解决方案1】:

对于每个表,您可以使用分析函数来获取最新的行,

ie

    select *
    from   
    ( select t.*,
             row_number() over ( partition by emp_id order by upd_dt desc ) as r
      from my_table t
    )
    where r = 1

Now you have three "tables" that only have 1 row per employee, thus a standard join will suffice.  Thus

with 
t1 as 
(
select *
    from   
    ( select t.*,
             row_number() over ( partition by emp_id order by upd_dt desc ) as r
      from my_table1 t
    )
    where r = 1
),
t2 as (
select *
    from   
    ( select t.*,
             row_number() over ( partition by emp_id order by upd_dt desc ) as r
      from my_table2 t
    )
    where r = 1
),
t3 as
(
select *
    from   
    ( select t.*,
             row_number() over ( partition by emp_id order by upd_dt desc ) as r
      from my_table3 t
    )
    where r = 1
)
select ...
from t1, t2, t3
where t1.emp_id = t2.emp_id
and t2.emp_id = t3.empid

这里有完整的分析函数教程

https://www.youtube.com/watch?v=0cjxYMxa1e4&list=PLJMaoEWvHwFIUwMrF4HLnRksF0H8DHGtt

【讨论】:

  • 谢谢康纳。让我检查一下这个解决方案,如果可行,我会尽快与您联系。
猜你喜欢
  • 2021-03-09
  • 2018-06-26
  • 1970-01-01
  • 2014-11-03
  • 2012-08-07
  • 1970-01-01
  • 1970-01-01
  • 2017-08-24
  • 2020-08-06
相关资源
最近更新 更多