查询语句加一列

feng1021 2007-06-17 02:56:22
现有一表(id,money)
查询结果按money降序排列,如:
id money
2 600
1 400
3 280
........
我现在想加一列,如:
名次 id money
第一名 2 600
第二名 1 400
第三名 3 280
.........
请问这个名次自动加一怎么做?求高手解答!!!
...全文
155 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanglei8 2007-06-17
  • 打赏
  • 举报
回复
select 名次='第'+convert(varchar(10),(select count(1) from test where a.money<=money))+'名',*
from test a
order by 名次
wanglei8 2007-06-17
  • 打赏
  • 举报
回复
create table test(id int,money int)
insert test select 2,600
union all select 1,400
union all select 3,2800

select 名次='第'+convert(varchar(10),(select count(1) from test where a.money<=money))+'名',*
from test a
order by (select count(1) from test where a.money<=money)
bill024 2007-06-17
  • 打赏
  • 举报
回复
select 名次='第'+cast(名次 as varchar)+'名',id,money from
(
select 名次=(select count(*) from test where money>=a.money),id,money from test a
)b
order by 名次

名次 id money
---------------------------------- ----------- -----------
第1名 3 2800
第2名 2 600
第3名 1 400

(所影响的行数为 3 行)
feng1021 2007-06-17
  • 打赏
  • 举报
回复
谢谢三位,名次已经出来了,可是没有体现出降序来呀,比如把280变成2800,他还是排在第三名
bill024 2007-06-17
  • 打赏
  • 举报
回复
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

名次 id money
---------------------------------- ----------- -----------
第1名 2 600
第2名 1 400
第3名 3 280

(所影响的行数为 3 行)
bill024 2007-06-17
  • 打赏
  • 举报
回复
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
昵称被占用了 2007-06-17
  • 打赏
  • 举报
回复
不用中文行不

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
lxzm1001 2007-06-17
  • 打赏
  • 举报
回复
select 名次=(select count(*) from table where a.money<money)+1,* from table a

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧