SQL 2000使用GROUP BY进行行编号

时间:2012-05-21 10:08:36

标签: sql-server-2000

这是我的表

################################
# 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升序排序

3 个答案:

答案 0 :(得分:1)

以下脚本应该让您入门。简而言之,脚本

  • 创建临时表,添加IDENTITY列作为rownumber
  • 将原始数据插入临时表
  • 为每个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

答案 1 :(得分: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

答案 2 :(得分: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。