当我运行此SQL代码时,我收到以下错误消息:
Msg 116, Level 16, State 1, Line 17
Only one expression can be specified in the select list when the subquery
is not introduced with EXISTS.
我想要的是每当从我的更新查询返回任何具有相同AgreementNo, ElementStartDate and DateSeqNo
的内容时,它会更新其中一个重复记录ElementStartDate
+ 1,这将删除副本。
Update [WASP_Mart_EmbassyActuarial].[dbo].[tblARM_OmegaSource]
SET ElementStartDate = ElementStartDate + 1
WHERE AgreementNo IN
(SELECT
count(*) as [Count],
[AgreementNo],
[ElementStartDate],
[DateSeqNo],
getdate() as Today
FROM [WASP_Mart_EmbassyActuarial].[dbo].[tblARM_OmegaSource]
GROUP BY
[AgreementNo],
[ElementStartDate],
[DateSeqNo]
HAVING COUNT(*) = 2)
答案 0 :(得分:1)
Sub查询返回多个列。您无法在运算符中使用多个列进行比较。
尝试以下查询:
Update [WASP_Mart_EmbassyActuarial].[dbo].[tblARM_OmegaSource]
SET ElementStartDate = ElementStartDate + 1
WHERE AgreementNo IN
( SELECT [AgreementNo]
FROM [WASP_Mart_EmbassyActuarial].[dbo].[tblARM_OmegaSource]
GROUP BY [AgreementNo] -- OR [AgreementNo],[ElementStartDate],[DateSeqNo]
HAVING COUNT(*) = 2)
答案 1 :(得分:1)
在AgreementNo, ElementStartDate, DateSeqNo
的分区中分配行号,并更新行号大于1的行:
WITH ranked AS (
SELECT
ElementStartDate,
rn = ROW_NUMBER() OVER (PARTITION BY AgreementNo, ElementStartDate, DateSeqNo
ORDER BY (SELECT 1)) -- actual order probably
-- doesn't matter here
FROM WASP_Mart_EmbassyActuarial.dbo.tblARM_OmegaSource
)
UPDATE ranked
SET ElementStartDate = ElementStartDate + rn - 1
WHERE rn > 1
;
此方法可以处理组中具有两个以上重复项的案例,当然,如果有很多重复项,它可能会开始生成新的重复项。
答案 2 :(得分:0)
也许试试这个:
Update [WASP_Mart_EmbassyActuarial].[dbo].[tblARM_OmegaSource]
SET ElementStartDate = ElementStartDate + 1
WHERE AgreementNo IN
(SELECT
AgreementNo
FROM [WASP_Mart_EmbassyActuarial].[dbo].[tblARM_OmegaSource]
GROUP BY
[AgreementNo],
[ElementStartDate],
[DateSeqNo]
HAVING COUNT(*) = 2)