需要检索 table A 中的所有记录,并且只检索 table B 中最后更新的一条记录
Need to retrieve all records in table A and only single one in table B that is the last updated
我必须在 TABLE_A 中检索某些记录 - 然后需要显示该行的最后一次更新时间 - 在 TABLE_B 中(但是,有很多记录在 TABLE_B). TABLE_A 的 TABLE_A.PK 是 ID 并通过 TABLE_B.LINK 链接到 TABLE_B,其中架构为:
TABLE_A
===================
ID NUMBER
DESC VARCHAR2
TABLE_B
===================
ID NUMBER
LINK NUMBER
LAST_DATE DATE
而实际的 table 数据将是:
TABLE_A
===================
100 DESCRIPTION0
101 DESCRIPTION1
TABLE_B
===================
1 100 12/12/2012
2 100 12/13/2012
3 100 12/14/2013
4 101 12/12/2012
5 101 12/13/2012
6 101 12/14/2013
所以,我需要读一些东西:
Result
====================
100 DESCRIPTION0 12/14/2013
101 DESCRIPTION1 12/14/2013
我尝试了不同的加入方式,但似乎没有任何效果:
select * from
(SELECT ID, DESC from TABLE_A WHERE ID >= 100) TBL_A
full outer join
(select LAST_DATE from TABLE_B WHERE ROWNUM = 1 order by LAST_DATE DESC) TBL_B
on TBL_A.ID = TBL_B.LINK;
最简单的方法是将 table_a
加入 table_b
:
的聚合查询
SELECT table_a.*, table_b.last_date
FROM table_a
LEFT JOIN (SELECT link, MAX(last_date) AS last_date
FROM table_b
GROUP BY link) table_b ON table_a.id = table_b.link
如果您只想要最近的日期,请考虑聚合和加入。额外级别的子查询无济于事。类似于:
select a.id, a.desc, max(last_date)
from table_a a join
table_b b
on a.id = b.link
where a.id >= 100
group by a.id, a.desc;
注意:我怀疑 full outer join
是否必要,但如果您的连接键在表之间不匹配,您可以保留它。也许 left join
是合适的。
我应该指出,如果您想要 b
中的更多字段,那么您最初倾向于使用 row_number()
是正确的。但查询看起来像:
select a.id, a.desc, max(last_date)
from table_a a left join
(select b.*, row_number() over (partition by link order by last_date desc) as seqnum
from table_b b
) b
on a.id = b.link and b.seqnum = 1
where a.id >= 100
group by a.id, a.desc;
我必须在 TABLE_A 中检索某些记录 - 然后需要显示该行的最后一次更新时间 - 在 TABLE_B 中(但是,有很多记录在 TABLE_B). TABLE_A 的 TABLE_A.PK 是 ID 并通过 TABLE_B.LINK 链接到 TABLE_B,其中架构为:
TABLE_A
===================
ID NUMBER
DESC VARCHAR2
TABLE_B
===================
ID NUMBER
LINK NUMBER
LAST_DATE DATE
而实际的 table 数据将是:
TABLE_A
===================
100 DESCRIPTION0
101 DESCRIPTION1
TABLE_B
===================
1 100 12/12/2012
2 100 12/13/2012
3 100 12/14/2013
4 101 12/12/2012
5 101 12/13/2012
6 101 12/14/2013
所以,我需要读一些东西:
Result
====================
100 DESCRIPTION0 12/14/2013
101 DESCRIPTION1 12/14/2013
我尝试了不同的加入方式,但似乎没有任何效果:
select * from
(SELECT ID, DESC from TABLE_A WHERE ID >= 100) TBL_A
full outer join
(select LAST_DATE from TABLE_B WHERE ROWNUM = 1 order by LAST_DATE DESC) TBL_B
on TBL_A.ID = TBL_B.LINK;
最简单的方法是将 table_a
加入 table_b
:
SELECT table_a.*, table_b.last_date
FROM table_a
LEFT JOIN (SELECT link, MAX(last_date) AS last_date
FROM table_b
GROUP BY link) table_b ON table_a.id = table_b.link
如果您只想要最近的日期,请考虑聚合和加入。额外级别的子查询无济于事。类似于:
select a.id, a.desc, max(last_date)
from table_a a join
table_b b
on a.id = b.link
where a.id >= 100
group by a.id, a.desc;
注意:我怀疑 full outer join
是否必要,但如果您的连接键在表之间不匹配,您可以保留它。也许 left join
是合适的。
我应该指出,如果您想要 b
中的更多字段,那么您最初倾向于使用 row_number()
是正确的。但查询看起来像:
select a.id, a.desc, max(last_date)
from table_a a left join
(select b.*, row_number() over (partition by link order by last_date desc) as seqnum
from table_b b
) b
on a.id = b.link and b.seqnum = 1
where a.id >= 100
group by a.id, a.desc;