从此查询中获取非重复行而不使用distinct关键字

时间:2014-03-20 03:49:18

标签: sql-server

我需要在不使用distinct关键字的情况下从下面的查询中过滤掉不同的行。

所以在CM.PfmFolderMstIpMap表中有重复的F.Id或PMMAP.PfmFolderFK Id,所以我想过滤掉不重复的那些

    SELECT  F.Id,
                     F.folderno, 
                     F.folderstatusdate, 
                    (SELECT codedesc 
                     FROM   cm.codetable 
                     WHERE  id = F.FolderStatusCode) 'FolderStatus', 
                    (SELECT codedesc 
                     FROM   cm.codetable 
                     WHERE  id = F.FolderLocationCode) 'Location', 
                    F.volume, 
                    F.sdexhibits, 
                    F.cost, 
                    F.remarks 
    FROM   cm.pfmfolder F WITH(nolock) 
           INNER JOIN cm.pfmfoldermstipmap PMMAP WITH(nolock) 
                   ON PMMAP.pfmfolderfk = F.id 
           INNER JOIN cm.mstip MST WITH(nolock) 
                   ON MST.id = PMMAP.mstipfk 
           LEFT JOIN tm.mstirsdmstipmap IRMAP WITH(nolock) 
                  ON IRMAP.mstipfk = MST.id 
           LEFT JOIN tm.mstirsd IRSD WITH(nolock) 
                  ON IRSD.id = IRMAP.mstirsdfk 
           LEFT JOIN tm.mstir IR WITH(nolock) 
                  ON IR.id = IRSD.mstirfk 
           LEFT JOIN tm.mstiabasicmark IABM 
                  ON IABM.basicmarknbr = MST.applnnbr 
           LEFT JOIN tm.mstia IA 
                  ON IA.id = IABM.mstiafk 
    WHERE  ( @FolderNbr IS NULL 
              OR F.folderno LIKE + @FolderNbr + '%' ) 
           AND ( @ApplnNbr IS NULL 
                  OR MST.applnnbr LIKE + @ApplnNbr + '%' ) 
           AND ( @IrNbr IS NULL 
                  OR IR.irnbr LIKE + @IrNbr + '%' ) 
           AND ( @IaNbr IS NULL 
                  OR IA.ianbr LIKE + @IaNbr + '%' ) 
           AND ( @FolderStatusCode IS NULL 
                  OR F.folderstatuscode = @FolderStatusCode ) 
           AND ( @FolderLocationCode IS NULL 
                  OR F.folderlocationcode = @FolderLocationCode ) 
           AND ( F.folderstatusdate >= @FolderStatusDateFrom 
                 AND F.folderstatusdate < @FolderStatusDateTo ) 
           AND MST.registrycode = @RegistryCode 
           AND PMMAP.deletedby IS NULL 

2 个答案:

答案 0 :(得分:1)

区别是最好的选择。否则,使用所有列

来使用Group

答案 1 :(得分:1)

Sam说,Distinct是最好的选择。否则,使用所有列

来使用Group

您也可以使用Row_Number功能

试试这个

SELECT * FROM
(
SELECT  F.Id,
                     F.folderno, 
                     F.folderstatusdate, 
                    (SELECT codedesc 
                     FROM   cm.codetable 
                     WHERE  id = F.FolderStatusCode) 'FolderStatus', 
                    (SELECT codedesc 
                     FROM   cm.codetable 
                     WHERE  id = F.FolderLocationCode) 'Location', 
                    F.volume, 
                    F.sdexhibits, 
                    F.cost, 
                    F.remarks,
                    Row_number() OVER(partition BY F.Id  ORDER BY (SELECT NULL) ASC) RN              
    FROM   cm.pfmfolder F WITH(nolock) 
           INNER JOIN cm.pfmfoldermstipmap PMMAP WITH(nolock) 
                   ON PMMAP.pfmfolderfk = F.id 
           INNER JOIN cm.mstip MST WITH(nolock) 
                   ON MST.id = PMMAP.mstipfk 
           LEFT JOIN tm.mstirsdmstipmap IRMAP WITH(nolock) 
                  ON IRMAP.mstipfk = MST.id 
           LEFT JOIN tm.mstirsd IRSD WITH(nolock) 
                  ON IRSD.id = IRMAP.mstirsdfk 
           LEFT JOIN tm.mstir IR WITH(nolock) 
                  ON IR.id = IRSD.mstirfk 
           LEFT JOIN tm.mstiabasicmark IABM 
                  ON IABM.basicmarknbr = MST.applnnbr 
           LEFT JOIN tm.mstia IA 
                  ON IA.id = IABM.mstiafk 
    WHERE  ( @FolderNbr IS NULL 
              OR F.folderno LIKE + @FolderNbr + '%' ) 
           AND ( @ApplnNbr IS NULL 
                  OR MST.applnnbr LIKE + @ApplnNbr + '%' ) 
           AND ( @IrNbr IS NULL 
                  OR IR.irnbr LIKE + @IrNbr + '%' ) 
           AND ( @IaNbr IS NULL 
                  OR IA.ianbr LIKE + @IaNbr + '%' ) 
           AND ( @FolderStatusCode IS NULL 
                  OR F.folderstatuscode = @FolderStatusCode ) 
           AND ( @FolderLocationCode IS NULL 
                  OR F.folderlocationcode = @FolderLocationCode ) 
           AND ( F.folderstatusdate >= @FolderStatusDateFrom 
                 AND F.folderstatusdate < @FolderStatusDateTo ) 
           AND MST.registrycode = @RegistryCode 
           AND PMMAP.deletedby IS NULL 
) AS T
WHERE RN = 1