用一个简单的dense_Rank()函数即可
------分组显示示例
IF NOT OBJECT_ID('[XXX]') IS NULL
DROP TABLE [XXX]
GO
CREATE TABLE [XXX](
[TID] int)
go
INSERT [XXX]
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 4
GO
----select * from XXX
/*
TID
1
1
2
4
*/
select TID,(dense_Rank() over (order by TID asc)) as num
from xxx
/*
TID num
1 1
1 1
2 2
4 3
*/
num的公式是??
distinct order by
select case A when A的值 then NUM
when A的值 then NUM
end
from 表名
你的意思是这个num跟列A的值完全一样?
select a.A,b.Num from Tb a inner join (select row_number() over (order by A) as Num,A from tb group by A) as b on a.A = b.A
select * from (select row_number() over (order by A) as Num,* from tableName) as tt
select Table1.A, temp_table1.Expr1 from (
SELECT A, row_number() over (ORDER BY A) AS Expr1
FROM (SELECT DISTINCT A
FROM Table1
) AS derivedtbl_1 ) temp_table1 join Table1
on temp_table1.A=Table1.A