使用时间戳对多个表进行左连接

发布于 2024-09-26 00:09:48 字数 679 浏览 5 评论 0原文

好吧,SQL 和 Oracle 专家,我正在尝试构建一个有点复杂的查询。

这是我当前的查询:

select distinct person_info.person_name
     table2.value,
     table3.value,
     table4.value,
     table5.value
from person_info
    left join table2 on table2.person_name=person_info.person_name
    left join table3 on table3.person_name=person_info.person_name
    left join table4 on table4.person_name=person_info.person_name
    left join table5 on table5.person_name=person_info.person_name;

每个表的主键都是 person_nametimestamp。现在我的问题是,如果表中存在同一个 person_name 的多个实例,那么我只想在最近的一个上left join。有谁知道如何将此行为添加到此查询中?我正在使用甲骨文。

谢谢!

Ok SQL and Oracle gurus I have a somewhat complicated query that I'm trying to build.

Here is my current query:

select distinct person_info.person_name
     table2.value,
     table3.value,
     table4.value,
     table5.value
from person_info
    left join table2 on table2.person_name=person_info.person_name
    left join table3 on table3.person_name=person_info.person_name
    left join table4 on table4.person_name=person_info.person_name
    left join table5 on table5.person_name=person_info.person_name;

The primary key for every table is both the person_name and a timestamp. Now my problem is that if multiple instances of the same person_name exist in a table then I only want to left join on the most recent one. Does anyone know how to add this behavior to this query? I am using Oracle.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是偏爱你 2024-10-03 00:09:48

尝试:

select distinct person_info.person_name
     t2.value,
     t3.value,
     t4.value,
     t5.value
from person_info
    left join (select t.*, row_number() over (partition by person_name order by timestamp_column desc) rowno from table2 t) t2 
         on t2.person_name=person_info.person_name and t2.rowno=1
    left join (select t.*, row_number() over (partition by person_name order by timestamp_column desc) rowno from table3 t) t3
         on t3.person_name=person_info.person_name and t3.rowno=1
    left join (select t.*, row_number() over (partition by person_name order by timestamp_column desc) rowno from table4 t) t4
         on t4.person_name=person_info.person_name and t4.rowno=1
    left join (select t.*, row_number() over (partition by person_name order by timestamp_column desc) rowno from table5 t) t5
         on t5.person_name=person_info.person_name and t5.rowno=1;

Try:

select distinct person_info.person_name
     t2.value,
     t3.value,
     t4.value,
     t5.value
from person_info
    left join (select t.*, row_number() over (partition by person_name order by timestamp_column desc) rowno from table2 t) t2 
         on t2.person_name=person_info.person_name and t2.rowno=1
    left join (select t.*, row_number() over (partition by person_name order by timestamp_column desc) rowno from table3 t) t3
         on t3.person_name=person_info.person_name and t3.rowno=1
    left join (select t.*, row_number() over (partition by person_name order by timestamp_column desc) rowno from table4 t) t4
         on t4.person_name=person_info.person_name and t4.rowno=1
    left join (select t.*, row_number() over (partition by person_name order by timestamp_column desc) rowno from table5 t) t5
         on t5.person_name=person_info.person_name and t5.rowno=1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文