替代FIRST_VALUE

时间:2016-09-16 15:00:18

标签: sql sql-server-2008 tsql

我想运行此查询:

;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,还有其他选择吗?

1 个答案:

答案 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