英文:
Last record of the same group
问题
我认为有类似的问题,但没有一个真正匹配我的情况。我尝试了左外连接来获取最新状态,但然后我无法根据父ID分组获取最新记录。
我有以下表格
文档表
id | 版本名称 | 父ID1 | 父ID2 | 时间戳 |
---|---|---|---|---|
UUI1 | A | 1 | 100 | 时间戳1 |
UUI2 | B | 1 | 100 | 时间戳2 |
UUI3 | C | 2 | 100 | 时间戳3 |
UUI4 | D | 2 | 100 | 时间戳4 |
UUI5 | E | 2 | 100 | 时间戳5 |
文档历史表
文档ID | 状态 | 时间戳 |
---|---|---|
UUI1 | 活跃 | 时间戳1 |
UUI1 | 不活跃 | 时间戳2 |
UUI2 | 不活跃 | 时间戳3 |
UUI2 | 活跃 | 时间戳4 |
UUI3 | 不活跃 | 时间戳3 |
UUI3 | 活跃 | 时间戳4 |
UUI4 | 不活跃 | 时间戳3 |
UUI4 | 活跃 | 时间戳4 |
UUI5 | 活跃 | 时间戳3 |
UUI5 | 不活跃 | 时间戳4 |
哪个查询给我以下表格
(根据parent_id1和parent_id2分组)
因此,具有相同的parent_id1和parent_id2的文档是同一文档的不同版本,因此我们只关心基于时间戳的最新版本。 我们还需要根据时间戳从历史表中获取它们的最新状态(一对多)
id | 版本名称 | 父ID1 | 父ID2 | 时间戳 | 状态 |
---|---|---|---|---|---|
UUI2 | B | 1 | 100 | 时间戳2 | 活跃 |
UUI5 | E | 2 | 100 | 时间戳5 | 不活跃 |
英文:
I think there are similar questions but none of them really matches my case. I tried left outer join to get the latest status but then I can't get the latest record based on the parent id grouping.
I have the following tables
Document table
id | version name | parnet_id1 | parent_id2 | timestamp |
---|---|---|---|---|
UUI1 | A | 1 | 100 | timestamp1 |
UUI2 | B | 1 | 100 | timestamp2 |
UUI3 | C | 2 | 100 | timestamp3 |
UUI4 | D | 2 | 100 | timestamp4 |
UUI5 | E | 2 | 100 | timestamp5 |
Document history table
document_id | status | timestamp |
---|---|---|
UUI1 | Active | timestamp1 |
UUI1 | Inactive | timestamp2 |
UUI2 | InActive | timestamp3 |
UUI2 | Active | timestamp4 |
UUI3 | InActive | timestamp3 |
UUI3 | Active | timestamp4 |
UUI4 | InActive | timestamp3 |
UUI4 | Active | timestamp4 |
UUI5 | Active | timestamp3 |
UUI5 | Inactive | timestamp4 |
What query gives me the following table
(group with parent_id1 and parent_id2)
So docs that have the same parent_id1 and parent_id2 are different versions of the same doc so we only interested in the latest version based on timestamp. We also need their latest status from the history table based on the timestamp (one to many)
id | version name | parnet_id1 | parent_id2 | timestamp | status |
---|---|---|---|---|---|
UUI2 | B | 1 | 100 | timestamp2 | Active |
UUI5 | E | 2 | 100 | timestamp5 | Inactive |
答案1
得分: 0
使用inner join
和group by
,我们可以按如下方式进行操作:
select h3.*, h2.status
from history h2
inner join (
select t.id, t.version_name, t.parnet_id1, t.parent_id2, max(h.timestamp) as timestamp
from history h
inner join (
select d.id, d.version_name, d.parnet_id1, d.parent_id2
from document d
inner join (
select parnet_id1, parent_id2, max(timestamp) as timestamp
from document
group by parnet_id1, parent_id2
) as s on s.parnet_id1 = d.parnet_id1 and s.parent_id2 = d.parent_id2 and s.timestamp = d.timestamp
) t on t.id = h.document_id
group by t.id, t.version_name, t.parnet_id1, t.parent_id2
) as h3 on h2.document_id = h3.id and h3.timestamp = h2.timestamp
英文:
Using inner join
and group by
we can do it as follows :
select h3.*, h2.status
from history h2
inner join (
select t.id, t.version_name, t.parnet_id1, t.parent_id2, max(h.timestamp) as timestamp
from history h
inner join (
select d.id, d.version_name, d.parnet_id1, d.parent_id2
from document d
inner join (
select parnet_id1, parent_id2, max(timestamp) as timestamp
from document
group by parnet_id1, parent_id2
) as s on s.parnet_id1 = d.parnet_id1 and s.parent_id2 = d.parent_id2 and s.timestamp = d.timestamp
) t on t.id = h.document_id
group by t.id, t.version_name, t.parnet_id1, t.parent_id2
) as h3 on h2.document_id = h3.id and h3.timestamp = h2.timestamp
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论