簡體   English   中英

根據最大ID連接表

[英]Joining tables based on the maximum id

我發現三個問題似乎都提出了類似的問題:

從行獲取最大值並連接到另一個表

通過連接表最大值僅選擇行

根據最大值連接表

但是我很難理解如何連接表,當最大值在id或index字段本身時,只保留其中一個表的最大行。

我正在尋找一個只需要連接的答案,因為這將允許解決方案在一個生成查詢的工具中工作,很容易讓它生成相應的連接,盡管子查詢可能也有點可行更多的努力。 我發現下面的答案特別有趣:

SELECT DISTINCT b.id, b.filename, a1.name
FROM a a1
JOIN b
  ON b.id = a1.id
LEFT JOIN a a2
  ON a2.id = a1.id
  AND a2.rank > a1.rank
WHERE a2.id IS NULL

但是,在我的情況下,排名列也是索引,例如“id”。 我無法比較平等和大於同時,因為它們永遠不會同時真實!

此外,可能使情況復雜化的是,我需要這種情況的典型查詢可以連接幾個表(3-5並不罕見)。 所以作為我的查詢的簡化示例:

SELECT
    table1.field1, table1.field2, table1.field3,
    table2.field1, table2.field2, table2.field3,
    table3.field1, table3.field2, table3.field3,
    table4.field1, table4.field2, table4.field3
FROM table1
INNER JOIN table2 ON
    table1.field1 = table2.field1
    AND table1.field2 = table2.field2
    AND table2.field3 < 0
INNER JOIN table3 ON
    table2.field1 = table3.field1
    AND table2.field4 = table3.field4
INNER JOIN table4 ON
    table1.field1 = table4.field1
    AND table1.field2 = table4.field2

我想要做的是通過僅為所有其他字段的每個唯一組合獲取具有最大id的行(例如MAX(table3.id))來消除表3中的重復。 也就是說,上面的查詢返回的內容如下:

+-------+-------+-------+---------+
| table1| table2| table4|table3   |
+-------+-------+-------+---------+
|  A    |   A   |   A   | 1,...   |
|  A    |   A   |   A   | 2,...   |
|  A    |   A   |   A   | 3,...   |
|  A    |   A   |   A   | MAX2,...|
|  B    |   B   |   B   | 1,...   |
|  B    |   B   |   B   | 2,...   |
|  B    |   B   |   B   | 3,...   |
|  B    |   B   |   B   | MAX2,...|
+-------+-------+-------+---------+

(我只是使用A和B來表示我正在討論table1,table2和table4中字段的所有相同值,以獲取特定的行集。)

我想把它減少到這個:

+-------+-------+-------+---------+
| table1| table2| table4|table3   |
+-------+-------+-------+---------+
|  A    |   A   |   A   | MAX1,...|
|  B    |   B   |   B   | MAX2,...|
+-------+-------+-------+---------+

您可以添加派生表以將TABLE3的匹配行減少為每組一個。 另一種方法是使用窗口函數但是你只要求一個JOIN

SELECT
    table1.field1, table1.field2, table1.field3,
    table2.field1, table2.field2, table2.field3,
    table3.field1, table3.field2, table3.field3,
    table4.field1, table4.field2, table4.field3
FROM table1
INNER JOIN table2 ON
    table1.field1 = table2.field1
    AND table1.field2 = table2.field2
    AND table2.field3 < 0
INNER JOIN table3 ON
    table2.field1 = table3.field1
    AND table2.field4 = table3.field4

--here is the added derived table. Change column names as needed
INNER JOIN (select UID, ID = max(ID) from Table3 group by UID) x
    on x.UID = table3.UID and x.mx = table3.ID

INNER JOIN table4 ON
    table1.field1 = table4.field1
    AND table1.field2 = table4.field2

或者,也許......如下所示。 這實際上取決於您的架構,並且使用示例數據很難理解。

INNER JOIN (select field1, field4, mx = max(ID) from Table3 group by field1, field4) x
    on x.field1 = table3.field1 and x.field4 = table3.field4 and x.mx = table3.ID

這是一個例子。 您會注意到最后三列對是相同的。 您只需要最后一個,即該分組的最大值(id)。 什么使得行與您的其他數據(不是您的主鍵,但您正在加入的)相關的唯一行是您希望包含在派生表和連接條件中的行。

declare @table table (id int identity(1,1), f1 char(1), f2 char(1))
insert into @table
values
('a','b'),
('a','c'),
('a','a'),
('b','b'),
('b','b'),
('b','b')

select * from @table

select t1.*
from @table t1
inner join 
    (select f1, f2, mx = max(id) from @table group by f1, f2) t2 on
    t1.f1 = t2.f1
    and t1.f2 = t2.f2
    and t1.id = t2.mx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM