日期:2014-05-19  浏览次数:20452 次

查询语句加一列
现有一表(id,money)
查询结果按money降序排列,如:
id       money
2           600
1           400
3           280
........
我现在想加一列,如:
名次           id     money
第一名         2         600
第二名         1         400
第三名         3         280
.........
请问这个名次自动加一怎么做?求高手解答!!!

------解决方案--------------------
不用中文行不

select
'第 '+cast((select count(*) from tablename where [money]> =a.[money]) as varchar)+ '名 ' as 名次,
a.id ,
a.[money]
from tablename a
order by a.[money] desc

------解决方案--------------------
create table test(id int,money int)
insert test select 2,600
union all select 1,400
union all select 3,280

select '第 '+cast(名次 as varchar)+ '名 ',id,money from
(
select 名次=(select count(*) from test where money> =a.money),id,money from test a
)b

drop table test