基于匹配的最大值的SQL联接表



我有三个id类似的表

表1:

id 数据
1
2
3
15

这是一个例子,显然您可以用正确的信息替换:

select TableOne.id
from TableOne
inner join (select max(id) as 'Id' from TableTwo) as TableTwo ON TableOne.Id = TableTwo.Id
inner join (select max(id) as 'Id' from TableThree) as TableThree ON TableTwo.Id = TableThree.Id
inner join (select max(id) as 'Id' from TableFour) as TableFour ON TableThree.Id = TableFour.Id

简单的连接,然后选择max就可以了:

-- sample data
WITH dataset(id) AS (
VALUES (1),
(2),
(3),
(5)
),
dataset1(id) AS (
VALUES (1),
(3)
),
dataset2(id) AS (
VALUES (1),
(2),
(3)
) 
-- query
SELECT max(d.id)
from dataset d
join dataset1 d1 on d.id = d1.id
join dataset1 d2 on d.id = d2.id

输出:

_col0
3

相关内容

  • 没有找到相关文章

最新更新