根据一列删除重复项

时间:2013-06-13 15:00:39

标签: sql sql-server

我必须连接SQL Server数据库中的多个表才能使用Report Builder中的字段。此SQL查询作为存储过程运行。问题是它返回了我认为重复的多行。

这是我的查询(取出了一些结构部分):

 ALTER PROCEDURE [dbo].[GetRollCallData] 
    @Ids        VARCHAR(255),
    @LexiconId  INT,
    @UUID       UNIQUEIDENTIFIER,
    @ReadOnly   INT
 AS


 DECLARE @TableCode INT
 SET @TableCode = 58


        EXEC InsertInSelectionCache @Ids, @UUID, @TableCode, 0

            SELECT [File].*, FileType2Lexicon.Label as FileTypeLabel, [People].DefaultPhone, CFV.Value as DOA, CFV.CustomRecordsetId
                FROM FileType2Lexicon, SelectionCache, [People], [File]
                INNER JOIN [CustomRecordSet]
                ON [CustomRecordset].RecordId = [File].Id
                INNER JOIN (SELECT Value, CustomFieldId, CustomRecordsetId FROM [CustomFieldValue]) CFV
                ON  [CustomRecordset].Id = CFV.CustomRecordsetId
                INNER JOIN (SELECT CustomFieldId, Label FROM [CustomField2Lexicon]) CF2L
                ON CF2L.CustomFieldId = CFV.CustomFieldId
                WHERE   [File].Id = SelectionCache.RecordId
                AND SelectionCache.UUID = @UUID
                AND SelectionCache.TableCode = @TableCode -- this is the code for File table  
                AND     [File].Id <> 0 
                AND     [File].FileTypeId = FileType2Lexicon.FileTypeId 
                AND     FileType2Lexicon.LexiconId = @LexiconId
                AND     [File].ClientIdString = [People].ClientIdString
                AND     CFV.Value <> ''
                AND     CF2L.Label = 'Date of Arrest'

最终返回:

Id  ShortFileNameMatterName Summary             FileTypeLabelDefaultPhone   DOA         CustomRecordsetId
4691    Bob State v.  Bob   RETAINED:1/1/2013   Criminal    (123) 242-8454  2013-01-16  6942
4691    Bob State v.  Bob   RETAINED:1/1/2013   Criminal    (123) 242-8454  2013-01-16  6948

很明显,由于recordsetId列,这些行是重复的。每个表行都有它自己的UUID。

有什么想法吗?我已经做了好几天了。

编辑: 这是我找到的解决方案。

    WITH RankedCTE AS(
        SELECT ROW_NUMBER() OVER(PARTITION BY [File].Id ORDER BY CustomRecordsetId DESC) AS RowNumber, [File].*, FileType2Lexicon.Label as FileTypeLabel, [People].DefaultPhone, [People].InvertedName, CFV.Value as DOA
            FROM FileType2Lexicon, SelectionCache, [People], [File]
            INNER JOIN [CustomRecordSet]
            ON [CustomRecordset].RecordId = [File].Id
            INNER JOIN (SELECT Value, CustomFieldId, CustomRecordsetId FROM [CustomFieldValue]) CFV
            ON  [CustomRecordset].Id = CFV.CustomRecordsetId
            INNER JOIN (SELECT CustomFieldId, Label FROM [CustomField2Lexicon]) CF2L
            ON CF2L.CustomFieldId = CFV.CustomFieldId
            WHERE   [File].Id = SelectionCache.RecordId
            AND SelectionCache.UUID = @UUID
            AND SelectionCache.TableCode = @TableCode -- this is the code for File table  
            AND     [File].Id <> 0 
            AND     [File].FileTypeId = FileType2Lexicon.FileTypeId 
            AND     FileType2Lexicon.LexiconId = @LexiconId
            AND     [File].ClientIdString = [People].ClientIdString
            AND     CFV.Value <> ''
            AND     (CF2L.Label = 'Date of Arrest')
        SELECT *
        FROM RankedCTE
        WHERE RowNumber = 1

2 个答案:

答案 0 :(得分:1)

你有没有考虑过使用......

SELECT DISTINCT [FILE].*, (etc)

甚至是......

GROUP BY [FILE].id

答案 1 :(得分:0)

如果问题是RecordSetId,则按其余列分组,并使用该列的聚合列。或者,通过聚合记录集来解决问题:

   SELECT [File].*, FileType2Lexicon.Label as FileTypeLabel, [People].DefaultPhone, CFV.Value as DOA, CFV.CustomRecordsetId
        FROM FileType2Lexicon, SelectionCache, [People], [File]
        INNER JOIN (select RecordId, max(id) as id
                    from [CustomRecordSet]
                    group by recordId
                   ) CustomRecordSet
        ON [CustomRecordset].RecordId = [File].Id
        INNER JOIN (SELECT Value, CustomFieldId, CustomRecordsetId FROM [CustomFieldValue]) CFV
        ON  [CustomRecordset].Id = CFV.CustomRecordsetId
        INNER JOIN (SELECT CustomFieldId, Label FROM [CustomField2Lexicon]) CF2L
        ON CF2L.CustomFieldId = CFV.CustomFieldId
        WHERE   [File].Id = SelectionCache.RecordId
        AND SelectionCache.UUID = @UUID
        AND SelectionCache.TableCode = @TableCode -- this is the code for File table  
        AND     [File].Id <> 0 
        AND     [File].FileTypeId = FileType2Lexicon.FileTypeId 
        AND     FileType2Lexicon.LexiconId = @LexiconId
        AND     [File].ClientIdString = [People].ClientIdString
        AND     CFV.Value <> ''
        AND     CF2L.Label = 'Date of Arrest'

此版本随意选择最大ID。