我有这个代码:
merge dbo.tblProblems as target
using (
select max(problemID), StationName, problemCode, max(ProblemCreateDate), count(*)
from dbo.tblProblems
group by StationName, problemCode
) as source(id, StationName, problemCode, maxdate, rowcount)
on (
target.problemID = source.problemID
and target.StationName = target.StationName
and target.problemCode = target.problemCode
)
when matched then
update set ProblemCreateDate = maxdate, probCount = rowcount
when not matched then delete ;
但它会产生这个错误:
消息 156,级别 15,状态 1,第 6 行
关键字“行计数”附近的语法不正确。
而这个版本的查询:
;WITH n AS
(
SELECT problemID, StationName, problemCode, ProblemCreateDate, probCount
c = COUNT(*) OVER (PARTITION BY StationName, problemCode),
rn = ROW_NUMBER() OVER
(
PARTITION BY StationName, problemCode ORDER BY ProblemCreateDate DESC, problemID DESC
)
FROM dbo.tblProblems
)
--SELECT problemID, StationName, problemCode, ProblemCreateDate, c
-- FROM n WHERE rn = 1;
UPDATE n SET probCount = c
WHERE rn = 1;
产生此错误:
消息 102,级别 15,状态 1,第 4 行
'=' 附近的语法不正确。