如何按字母顺序显示row_number

时间:2013-12-20 06:39:30

标签: asp.net sql .net sql-server sql-server-2008

SELECT Row_number()
         OVER (
           ORDER BY setsid) AS Set_No,
       setsid,
       testid
FROM   examsets
WHERE  testid = 4

结果是: -

Set_No               
------------------- 
1                    
2                    
3                   
4                    
5                    
6                   
7   

我想要结果为: -

Set_No               
------------------- 
a                    
b                    
c                   
d                    
e                    
f                   
g 

我该怎么做?

2 个答案:

答案 0 :(得分:1)

你可以用它。它不会处理返回超过26行的情况。

char(96+row_number() over(order by setsid))

AA转到ZZ将为您提供676个不同的值。

select char(((T.N / 26) % 26) + 65) + char((T.N % 26) + 65) as Set_No
from (
     select row_number() over(order by setsid) - 1 as N
     from YourTable
     ) as T

SQL Fiddle

答案 1 :(得分:0)

SELECT 
      char(ascii(row_number() over (order by setsid))+48) as Set_No       
      setsid,
      testid
FROM   
      examsets
WHERE  
      testid = 4