2

这是我的桌子

################################
# id_formfield # ID # int_Sort #
################################
#      1       # 1  #     2    #
#      2       # 1  #     3    #
#      3       # 1  #     4    #
#      4       # 1  #     4    #
#      5       # 1  #     4    #
#      6       # 2  #     1    #
#      7       # 2  #     3    #
#      8       # 2  #     3    #
#      9       # 2  #     4    #

正如您所看到的,我的 int_sort 列以某种方式被相同的数字弄乱了,我想使它与行号一起排序,并在 ID 更改时重置行号。这应该是结果:

############################################
# id_formfield # ID # int_Sort # rownumber #
############################################
#      1       # 1  #     2    #     1     #
#      2       # 1  #     3    #     2     #
#      3       # 1  #     4    #     3     #
#      4       # 1  #     4    #     4     #
#      5       # 1  #     4    #     5     #
#      6       # 2  #     1    #     1     #
#      7       # 2  #     3    #     2     #
#      8       # 2  #     3    #     3     #
#      9       # 2  #     4    #     4     #

编辑: id_formfield 是我的主键,并按 int_sort 升序排列

4

3 回答 3

1

以下脚本应该可以帮助您入门。简而言之,脚本

  • 创建一个临时表,添加一IDENTITY列作为行号
  • 将原始数据插入临时表
  • 用于MIN(Rownumber)每个ID获得偏移量。
  • JOIN具有计算出的偏移量的临时表以重新开始每个组的计数。

SQL 语句

CREATE TABLE #TempTable (Rownumber INTEGER IDENTITY(1, 1), ID INTEGER)
SET IDENTITY_INSERT #TempTable OFF

INSERT INTO #TempTable
SELECT      *
FROM        YourOriginalTable
ORDER BY    ID, int_Sort

SELECT    t.ID, t.Rownumber, t.Rownumber - o.Offset
FROM      #TempTable t
          INNER JOIN (
            SELECT    ID, MIN(Rownumber) - 1 AS Offset
            FROM      #TempTable
            GROUP BY  ID
          ) o ON o.ID = t.ID            


DROP TABLE #TempTable

测试脚本

DECLARE @YourTable TABLE (ID VARCHAR(1))
CREATE TABLE #TempTable (Rownumber INTEGER IDENTITY(1, 1), ID INTEGER)

SET IDENTITY_INSERT #TempTable OFF
INSERT INTO @YourTable (ID) VALUES (1)
INSERT INTO @YourTable (ID) VALUES (1)
INSERT INTO @YourTable (ID) VALUES (1)
INSERT INTO @YourTable (ID) VALUES (1)
INSERT INTO @YourTable (ID) VALUES (1)
INSERT INTO @YourTable (ID) VALUES (2)
INSERT INTO @YourTable (ID) VALUES (2)
INSERT INTO @YourTable (ID) VALUES (2)
INSERT INTO @YourTable (ID) VALUES (2)

INSERT INTO #TempTable
SELECT      *
FROM        @YourTable
ORDER BY    ID

SELECT    t.ID, t.Rownumber, t.Rownumber - o.Offset
FROM      #TempTable t
          INNER JOIN (
            SELECT    ID, MIN(Rownumber) - 1 AS Offset
            FROM      #TempTable
            GROUP BY  ID
          ) o ON o.ID = t.ID            


DROP TABLE #TempTable
于 2012-05-21T10:30:55.597 回答
1

我假设您的表中有一个主键被调用PK并且您想要更新该列int_sort

select cast(PK as int) as PK,
       ID,
       identity(int, 1, 1) as rownumber
into #T
from YourTable
order by ID, int_sort

update YourTable
set int_sort = T.rownumber - T2.rownumber + 1 
from #T as T
  inner join (
               select ID, min(rownumber) as rownumber
               from #T
               group by ID
             ) as T2
    on T.ID = T2.ID
where T.PK = YourTable.PK


drop table #T
于 2012-05-21T10:52:00.137 回答
1

我假设您的主键列名为idpk. 您可以使用以下查询获取所需的行号:

SELECT ts.id, ts.int_sort,
  (SELECT count(*) FROM tosort
    WHERE id = ts.id AND int_sort <= ts.int_sort
      AND idpk <= ts.idpk) AS row_number
  FROM tosort AS ts
 ORDER BY ts.id, ts.int_sort, ts.idpk;

我在SQL Fiddle创建了一个示例案例,尽管那里只有 2008 R2 可用。我希望这将适用于 SQL Server 2000。

于 2012-05-21T10:54:58.630 回答