2

我有一个视图,其中包含一个文档 ID 列和一个日期列以及十几个与此问题无关的其他列。可以有多行具有相同的文档 ID,但日期通常不同。这表示它是同一个文档,只是它的修订版。问题是如果我有两行文档 ID 和日期相同,我都会得到。我只想得到一个。不管是哪一个,只要我得到一个。

以下是文档 ID 和日期相同的重复项。

SELECT FSD.*
FROM vFSD FSD
INNER JOIN
    (
    SELECT InternalID, MAX(FileLastUploadedDate) AS FileLastUploadedDate
    FROM vFSD
    GROUP BY InternalID
    ) gFSD ON FSD.InternalID = gFSD.InternalID AND FSD.FileLastUploadedDate = gFSD.FileLastUploadedDate

我也用 DISTINCT 尝试过,但没有解决问题。

SELECT DISTINCT FSD.*
FROM vFSD FSD
INNER JOIN
    (
    SELECT DISTINCT InternalID, MAX(FileLastUploadedDate) AS FileLastUploadedDate
    FROM vFSD
    GROUP BY InternalID
    ) gFSD ON FSD.InternalID = gFSD.InternalID AND FSD.FileLastUploadedDate = gFSD.FileLastUploadedDate
4

1 回答 1

5

如果两个与相同的最大值并列,您可以使用ROW_NUMBER仅带回一个任意FileLastUploadedDateInternalID

WITH CTE
     AS (SELECT *,
                ROW_NUMBER() OVER (PARTITION BY InternalID 
                                       ORDER BY FileLastUploadedDate DESC) AS RN
         FROM   vFSD)
SELECT InternalID,
       FileLastUploadedDate
       /*Other desired columns*/ 
FROM   CTE
WHERE  RN = 1 
于 2013-06-04T22:23:30.880 回答