0

我有 3 个名为 ICD10、Claims 和 AuthHeader 的表。这 3 个表全部连接在一起以创建 1 个用于访问我的 SSAS Cube 的视图表。在 ICD10 中有重复的行,需要删除。如何在不弄乱与 ICD10 表连接的主键的情况下删除重复项?我看过这个脚本,但我认为这不是我需要的

DECLARE @table TABLE (
      id INT IDENTITY(1, 1)
    , data VARCHAR(20)
    )

DELETE  FROM @table

FROM    @table o

        INNER JOIN ( SELECT data
                     FROM   @table
                     GROUP BY data
                     HAVING COUNT(*) > 1
                   ) f ON o.data = f.data
        LEFT OUTER JOIN ( SELECT    [id] = MAX(id)
                          FROM      @table
                          GROUP BY  data
                          HAVING    COUNT(*) > 1
                        ) g ON o.id = g.id
WHERE   g.id IS NULL 
4

1 回答 1

0

所以这就是我为解决问题所做的...

---------------------------------------
-- INSERT DUPLICATES INTO TEMP TABLE --
--------------------------------------------------------
IF OBJECT_ID('tempdb..#TEMP') IS NOT NULL 
DROP TABLE #TEMP

SELECT DiagnosisCode
      ,DiagnosisDesc
      ,icd10ThreeCharacter
      ,icd10AdditionalGroup
      ,icd10DiseaseGroup
      ,icd10Level2
      ,icd10Chapter
      ,PMBCDLCode
      ,PMBCDLDescription
      ,PMBCDLRank

INTO #TEMP        
FROM MSOEDW.shared.ICD10
GROUP
  BY DiagnosisCode
    ,DiagnosisDesc
    ,icd10ThreeCharacter
    ,icd10AdditionalGroup
    ,icd10DiseaseGroup
    ,icd10Level2
    ,icd10Chapter
    ,PMBCDLCode
    ,PMBCDLDescription
    ,PMBCDLRank

 HAVING COUNT(DiagnosisCode) > 1

 SELECT [DiagnosisCode]
       ,[DiagnosisDesc]
       ,[icd10ThreeCharacter]
       ,[icd10AdditionalGroup]
       ,[icd10DiseaseGroup]
       ,[icd10Level2]
       ,[icd10Chapter]
       ,[PMBCDLCode]
       ,[PMBCDLDescription]
       ,[PMBCDLRank]
  FROM #TEMP

-----------------------
-- DELETE DUPLICATES --
---------------------------------------------------------
DELETE 
FROM [MSOEDW].[shared].[ICD10]
WHERE EXISTS (
SELECT DISTINCT TMP.*
FROM [MSOEDW].[shared].[ICD10] ICD  
JOIN #TEMP TMP
  ON ICD.DiagnosisCode = TMP.DiagnosisCode
JOIN [MSOEDW].[auth].[AuthHeader] head 
  ON ICD.[icd10Key] = head.[icd10Key]
JOIN [MSOEDW].[claim].[Claim] clm
  ON icd.[icd10Key] = clm.[icd10Key]
  )
------------------------------------------------------------

希望这将在不久的将来帮助其他有同样问题的人

于 2013-01-17T11:37:10.517 回答