gpt4 book ai didi

sql - 如何在 SQL MERGE 语句中使用 GROUP BY 子句?

转载 作者:行者123 更新时间:2023-12-05 00:32:17 26 4
gpt4 key购买 nike

我正在尝试合并两个表,我想使用 GROUP BY 来修复以下错误:
The MERGE statement attempted to UPDATE or DELETE the same row more than once. This happens when a target row matches more than one source row. A MERGE statement cannot UPDATE/DELETE the same row of the target table multiple times. Refine the ON clause to ensure a target row matches at most one source row, or use the GROUP BY clause to group the source rows.
GROUP BY 子句到底会去哪里?

MERGE dbo.MyTarget targ
USING dbo.MySource src
ON (targ.Identifier = src.Identifier
AND targ.Name = src.ConstituentName
AND targ.Ticker = src.ConstituentTicker
AND (targ.CUSIP = src.CUSIP OR targ.ISIN = src.ISIN OR targ.SEDOL = src.SEDOL))
WHEN MATCHED THEN
-- update values
;

最佳答案

像这样的东西:

MERGE dbo.MyTarget targ
USING (SELECT ... FROM dbo.MySource GROUP BY .....) src
ON (targ.Identifier = src.Identifier
AND targ.Name = src.ConstituentName
AND targ.Ticker = src.ConstituentTicker
AND (targ.CUSIP = src.CUSIP OR targ.ISIN = src.ISIN OR targ.SEDOL = src.SEDOL))
WHEN MATCHED THEN
-- update values
;

关于sql - 如何在 SQL MERGE 语句中使用 GROUP BY 子句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13727437/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com