CREATE TABLE #temp (
  Code1  VARCHAR(10),
  Code2  VARCHAR(7)
);
go

INSERT INTO #temp
  SELECT '330200101', '3302001' UNION ALL
  SELECT '330200102', '3302001' UNION ALL
  SELECT '330200103', '3302001' UNION ALL
  SELECT '330200105', '3302001' UNION ALL
  SELECT '330200201', '3302002' UNION ALL
  SELECT '330200202', '3302002' UNION ALL
  SELECT '330200203', '3302002' UNION ALL
  SELECT '330200204', '3302002' UNION ALL
  SELECT '330200205', '3302002'
GO


1> SELECT
2>   (SELECT RIGHT('000' + CAST( COUNT(1) AS VARCHAR ), 3)
3>    FROM #temp sub
4>    WHERE
5>      sub.Code2 = main.Code2
6>      AND sub.Code1 <= main.Code1
7>   ) AS 序号,
8>   Code1  AS  代码1,
9>   Code2  AS  代码2
10> FROM
11>   #temp main;
12> GO

序号     代码1        代码2
------ ---------- -------
001    330200101  3302001
002    330200102  3302001
003    330200103  3302001
004    330200105  3302001
001    330200201  3302002
002    330200202  3302002
003    330200203  3302002
004    330200204  3302002
005    330200205  3302002