SQL分组编号
IF OBJECT_ID(N'tempdb..#tmp') IS NOT NULL
DROP TABLE #tmp
CREATE TABLE #tmp ( aaa VARCHAR(10), bbb INT )
INSERT INTO #tmp
( aaa, bbb )
VALUES ( '11111', 5 )
INSERT INTO #tmp
( aaa, bbb )
VALUES ( '11111', 3 )
INSERT INTO #tmp
( aaa, bbb )
VALUES ( '11111', 2 )
INSERT INTO #tmp
( aaa, bbb )
VALUES ( '44444', 1 )
INSERT INTO #tmp
( aaa, bbb )
VALUES ( '44444', 3 )
SELECT * FROM #tmp
select aaa,bbb,row_number()over(partition by aaa order by aaa) rn from #tmp
IF OBJECT_ID(N'tempdb..#tmp') IS NOT NULL
DROP TABLE #tmp
CREATE TABLE #tmp ( aaa VARCHAR(10), bbb INT )
INSERT INTO #tmp
( aaa, bbb )
VALUES ( '11111', 5 )
INSERT INTO #tmp
( aaa, bbb )
VALUES ( '22222', 3 )
INSERT INTO #tmp
( aaa, bbb )
VALUES ( '33333', 2 )
INSERT INTO #tmp
( aaa, bbb )
VALUES ( '44444', 1 )
INSERT INTO #tmp
( aaa, bbb )
VALUES ( '55555', 3 )
SELECT *
FROM #tmp
SELECT b.aaa ,
b.bbb ,
a.number
FROM #tmp b
JOIN master.dbo.spt_values a ON 1 = 1
WHERE a.number > 0
AND a.number <= 2000
AND a.type = 'p'
AND a.number <= b.bbb