我正在使用以下查询;但它不起作用。挑战在于 e.Value 可以为每个文档(因此每个设备)返回多个结果,我想用最新的 e.Value 更新 Equipment.LastCheck(因此 ORDER BY d.CreatedDate)。
无论我尝试什么, Equipment.LastCheck 都不会更新。谁能告诉我我做错了什么?
WITH cte
AS (SELECT eq.LastCheck,
e.Value AS Result,
ROW_NUMBER() OVER (ORDER BY d.CreatedDate DESC) AS Rn
FROM Equipment eq
INNER JOIN Documents d
ON d.EquipmentId = eq.EquipmentId
INNER JOIN Elements e
ON e.ElementId = d.ElementId
WHERE e.Name = 'CheckDate'
AND e.Value IS NOT NULL)
UPDATE cte
SET LastCheck = Result;