我有一个包含数据组的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
答案 0 :(得分:2)
您可以使用自联接更新您的表格:
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'
答案 1 :(得分: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)
Here is a demo让你尝试。
答案 2 :(得分: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'
答案 3 :(得分: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