0

我有一个包含一组数据的 sql server (2008) 表。

Source    PersonId    Date      Description   Code     IsDup
------    --------    -----     -----------   ------   ----- 
EHR       1234        1/1/2012  Fever         120.12
EHR       1234        6/1/2012  Fracture      101.00
EHR       1234        11/4/2012 Hypertension  223.15
RAM       1234        1/1/2012  Fever         120.12              <-- Duplicate
RAM       1234        6/1/2012  Fracture      101.00              <-- Duplicate
RAM       1234        4/1/2012  Diabetic      601.00
TAR       1234        2/1/2012  Asthma        456.00
TAR       1234        1/1/2012  Fever         120.12              <-- Duplicate

我需要比较不同组之间的数据。“EHR”作为主组,我需要检查是否有任何其他组的数据与表中“EHR”主组中的数据完全匹配。然后它应该用 1 更新 IsDup 列。

预期结果:

Source    PersonId    Date      Description   Code     IsDup
------    --------    -----     -----------   ------   ----- 
EHR       1234        1/1/2012  Fever         120.12
EHR       1234        6/1/2012  Fracture      101.00
EHR       1234        11/4/2012 Hypertension  223.15
RAM       1234        1/1/2012  Fever         120.12     1
RAM       1234        6/1/2012  Fracture      101.00     1
RAM       1234        4/1/2012  Diabetic      601.00
TAR       1234        2/1/2012  Asthma        456.00
TAR       1234        1/1/2012  Fever         120.12     1

我知道如何检查表中的重复项,但不确定如何进行比较以保持一组静态。

我从一个stackoverflow线程中得到了这个来识别重复,但是如何添加分组比较:

with x as   (select  *, rn = row_number()
            over(PARTITION BY [PersonId], [Date], [Description], [Code] order by [PersonId], [Date], [Description], [Code])
            from    Results)

select * from x
where rn > 1
4

4 回答 4

2

您可以使用 self join 更新您的表:

update r1 set isDup = 1
from results r1 join results r2 on 
     r1.PersonId = r2.PersonId and r1.Date = r2.Date and 
     r1.Description = r2.Description and r1.Code = r2.Code 
where r1.Source <> 'EHR' and r2.Source = 'EHR'
于 2013-03-15T16:14:27.207 回答
1

这应该这样做:

UPDATE A
SET IsDup = 1 
FROM YourTable A
WHERE [Source] != 'EHR'
AND EXISTS (SELECT 1 FROM YourTable
            WHERE [Source] = 'EHR'
            AND PersonId = A.PersonId 
            AND [Date] = A.[Date]
            AND Description = A.Description
            AND Code = A.Code)

这是一个演示供您尝试。

于 2013-03-15T16:14:14.130 回答
0

试试这个:

;With rootQuery as 
(
Select  SOURCE, PersonId, Date, Description, Code
From    MedicalHistory 
Where   Source = 'EHR'
)
Update mhd
Set IsDuplicate = 1
From    rootquery mh 
Join    MedicalHistory mhd on mh.PersonId = mhd.PersonId
Where   mh.Description = mhd.Description
And     mh.Code = mhd.Code
And         mh.Date = mhd.Date
And     mhd.Source != 'EHR'
于 2013-03-15T16:18:00.140 回答
0

请试试这个..

   update tab
   set tab1.isDup=1
   from table1 tab1, table1 tab2
   where 
   tab1.PersonId=tab1.PersonId and
   tab1.Date=tab2.Date and
   tab1.desription=tab2.desription and
   tab1.Code=tab2.Code and
   tab1.Source != tab2.source 
于 2013-04-24T06:39:41.963 回答