1

我有这样的桌子

 id buildingId order  title
 --------------------------
  1      2      null  test1
  2      2      null  test2
  3      2      null  test3
  4      3      null  test4
  5      3      null  test5
  6      5      null  test6

我需要计算每一行的订单价值。我需要在执行 sql 查询后得到这个表

 id buildingId order  title
  1      2      0     test1
  2      2      1     test2
  3      2      2     test3
  4      3      0     test4
  5      3      1     test5
  6      5      0     test6

我怎样才能做到这一点?

4

1 回答 1

1
WITH recordList
AS
(
    SELECT  ID, buildingID, [order], title,
            ROW_NUMBER() OVER (PARTITION BY buildingID
                                ORDER BY ID) rn
    FROM    tableName
)
UPDATE recordList
SET [order] = rn - 1
于 2013-02-25T07:19:56.793 回答