MS SQL在使用DISTINCT / INNER JOIN时删除重复项

时间:2016-08-17 12:55:26

标签: sql-server duplicates inner-join distinct sql-server-2014

嘿所有我使用以下查询来获取一些数据:

SELECT DISTINCT 
    CAST(APL.ProjectID AS nvarchar(4000))         AS ProjectID,
    CAST(APL.StatusOrder AS nvarchar(4000))       AS StatusOrder,
    CAST(APL.DateModified AS nvarchar(4000))      AS DateModified,  
    CAST(APL.PType AS nvarchar(4000))             AS PType,
    CAST(APL.PName AS nvarchar(4000))             AS PName,
    CAST(APL.PostDate AS nvarchar(4000))          AS PostDate,
    CAST(APL.TBillID AS nvarchar(4000))           AS TBillID,
    CAST(APL.Rate AS nvarchar(4000))              AS Rate,
    CAST(APL.Amount AS nvarchar(4000))            AS Amount,
    CAST(P.PContact AS nvarchar(4000))            AS PContact,
    CAST(P.PEMail AS nvarchar(4000))              AS PEMail,
    CAST(P.PPhone AS nvarchar(4000))              AS PPhone,
    CAST(P.PFax AS nvarchar(4000))                AS PFax,
    CAST(APL.CompDate AS nvarchar(4000))          AS CompDate,
    CAST(PS.Comments AS nvarchar(4000))           AS Comments,
    CAST(PS.PSID AS nvarchar(4000))               AS PSID,
    CAST(PS.Status AS nvarchar(4000))             AS Status 
FROM [billing].[dbo].[ActiveProjList]         AS APL 
INNER JOIN [billing].[dbo].[tblPro]           AS P ON APL.ProjectID = P.ProjectID 
INNER JOIN [billing].[dbo].[tblPStatus]       AS PS ON APL.ProjectID = PS.ProjectID

但是,这会返回 141 的记录数。返回的正确的记录应仅 46

我可以用来查看是否有任何重复的列 ProjectID

2 个答案:

答案 0 :(得分:2)

SELECT DISTINCT适用于SELECT中的所有列。通常,使用ROW_NUMBER()

获得所需的结果
with t as (
      SELECT CAST(APL.ProjectID AS nvarchar(4000))         AS ProjectID,
             . . .
      FROM [billing].[dbo].[ActiveProjList] APL INNER JOIN
           [billing].[dbo].[tblPro] P
           ON APL.ProjectID = P.ProjectID INNER JOIN
           [billing].[dbo].[tblPStatus] PS
           ON APL.ProjectID = PS.ProjectID
     )
select t.*
from (select t.*,
             row_number() over (partition by ProjectId
                                order by datemodified desc
                               ) as seqnum
      from t
     ) t
where seqnum = 1;

这将获取与最新datemodified值相关联的值 - 这似乎是一种合理的方法,没有关于如何在重复项中进行选择的信息。

答案 1 :(得分:2)

试试这个

SELECT DISTINCT 
     CAST(APL.ProjectID AS nvarchar(4000))         AS ProjectID,
     MAX(CAST(APL.StatusOrder AS nvarchar(4000)))       AS StatusOrder,
     MAX(CAST(APL.DateModified AS nvarchar(4000)))      AS DateModified,  
     MAX(CAST(APL.PType AS nvarchar(4000)))             AS PType,
     MAX(CAST(APL.PName AS nvarchar(4000)))             AS PName,
     MAX(CAST(APL.PostDate AS nvarchar(4000)))          AS PostDate,
     MAX(CAST(APL.TBillID AS nvarchar(4000)))           AS TBillID,
     MAX(CAST(APL.Rate AS nvarchar(4000)))              AS Rate,
     MAX(CAST(APL.Amount AS nvarchar(4000)))            AS Amount,
     MAX(CAST(P.PContact AS nvarchar(4000)))            AS PContact,
     MAX(CAST(P.PEMail AS nvarchar(4000)))              AS PEMail,
     MAX(CAST(P.PPhone AS nvarchar(4000)))              AS PPhone,
     MAX(CAST(P.PFax AS nvarchar(4000))  )              AS PFax,
     MAX(CAST(APL.CompDate AS nvarchar(4000)))          AS CompDate,
     MAX(CAST(PS.Comments AS nvarchar(4000)))           AS Comments,
     MAX(CAST(PS.PSID AS nvarchar(4000)))               AS PSID,
     MAX(CAST(PS.Status AS nvarchar(4000)))             AS Status 
FROM 
      [billing].[dbo].[ActiveProjList]         AS APL 
INNER JOIN 
      [billing].[dbo].[tblPro]           AS P ON APL.ProjectID = P.ProjectID 
INNER JOIN 
      [billing].[dbo].[tblPStatus]       AS PS ON APL.ProjectID = PS.ProjectID
GROUP BY 
      APL.ProjectID