我想运行此查询:
;WITH ToUpdate AS (
SELECT Id1, Id2,
FIRST_VALUE(Id1) OVER (PARTITION BY Id2 ORDER BY Id1) AS newValue
FROM mytable
)
UPDATE ToUpdate
SET Id2 = newValue
但是在SQL Server 2008中没有FIRST_VALUE,还有其他选择吗?
答案 0 :(得分:1)
试试这个:使用OUTER APPLY
;WITH ToUpdate AS (
SELECT Id1, Id2,
M.newValue AS newValue
FROM mytable t1
OUTER APPLY(
SELECT top 1 t2.ID1 AS newValue
FROM mytable t2 WHERE t1.Id2=t2.Id2
ORDER BY t2.ID1
)M
)
UPDATE ToUpdate
SET Id2 = newValue