我有这个select语句,它根据某些条件在我的表中找到重复项。
选择是:
select DIVISION, FIPS_COUNTY_CODE, LAST, SUFFIX, FIRST, TITLE, BIRTHDATE, count(*)
from work
group by DIVISION, FIPS_COUNTY_CODE, LAST, SUFFIX, FIRST, TITLE, BIRTHDATE
having count(*) > 1
我想将此转换为将字母附加到表格中的字段的更新。例如,
UPDATE [WORK] SET BAD_CODES = BAD_CODES & 'D'
有关如何制作此更新声明的任何想法?
答案 0 :(得分:0)
我相信这应该做到,但我对Access不是很熟悉:
UPDATE [WORK]
SET BAD_CODES = BAD_CODES & 'D'
WHERE EXISTS (SELECT DIVISION, FIPS_COUNTY_CODE, LAST, SUFFIX, FIRST, TITLE, BIRTHDATE, COUNT(*)
FROM work b
WHERE [WORK].DIVISION = b.DIVISION
AND [WORK].FIPS_COUNTY_CODE = b.FIPS_COUNTY_CODE
AND [WORK].LAST = b.LAST
AND [WORK].SUFFIX = b.SUFFIX
AND [WORK].FIRST = b.FIRST
AND [WORK].TITLE = b.TITLE
AND [WORK].BIRTHDATE = b.BIRTHDATE
GROUP BY DIVISION, FIPS_COUNTY_CODE, LAST, SUFFIX, FIRST, TITLE, BIRTHDATE
HAVING COUNT(*) > 1
)
这应该是等效的,但是如果您想在更新中使用子查询中的值,则非常有用:
UPDATE T
SET BAD_CODES = BAD_CODES & 'D'
FROM work AS T
JOIN (SELECT DIVISION, FIPS_COUNTY_CODE, LAST, SUFFIX, FIRST, TITLE, BIRTHDATE, COUNT(*)
FROM work
GROUP BY DIVISION, FIPS_COUNTY_CODE, LAST, SUFFIX, FIRST, TITLE, BIRTHDATE
HAVING COUNT(*) > 1
) AS S
ON T.DIVISION = S.DIVISION
AND T.FIPS_COUNTY_CODE = S.FIPS_COUNTY_CODE
AND T.LAST = S.LAST
AND T.SUFFIX = S.SUFFIX
AND T.FIRST = S.FIRST
AND T.TITLE = S.TITLE
AND T.BIRTHDATE = S.BIRTHDATE