一个查询问题

Mirror然 2007-06-26 04:39:47
原表为tabel1
id name money
1 A 20
2 B 40
3 C 30
想根据查询得到如下显示
排名 id name money
1 1 A 20
2 3 C 30
3 2 B 40
后面还有更多数据,怎样写好点?(money是降序) 学习
...全文
177 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mirror然 2007-06-26
  • 打赏
  • 举报
回复
谢谢了^_^! 结贴了!
Zack999 2007-06-26
  • 打赏
  • 举报
回复
如果money有重复值?
RunUpwind 2007-06-26
  • 打赏
  • 举报
回复
select 排名=identity(int),* into #tmp from tabel1 order by money

select * from #tmp

drop table #tmp
mugua604 2007-06-26
  • 打赏
  • 举报
回复
个人感觉temptable速度比较快,但要牺牲空间!
RunUpwind 2007-06-26
  • 打赏
  • 举报
回复

--不如用个临时表

select 排名=identity(int),* into #tmp from tabel1 order by money desc

select * from #tmp

drop table #tmp
mugua604 2007-06-26
  • 打赏
  • 举报
回复
select identity(int,1,1) as 排序,* INTO temp from 表 order by money

select * from temp
昵称被占用了 2007-06-26
  • 打赏
  • 举报
回复
?(money是降序)

好像列出的实际是升序
昵称被占用了 2007-06-26
  • 打赏
  • 举报
回复
select 排名=(select count(*) from tabel1 where money<=a.money),id,name,money from tabel1 a
order by a.money
bill024 2007-06-26
  • 打赏
  • 举报
回复
改下
select 排名=(select count(1)+1 from tabel1 where money<a.money),id,name,money from tabel1 a
order by 排名
bill024 2007-06-26
  • 打赏
  • 举报
回复
排名 id name money
----------- ----------- ---------- -----------
1 1 A 20
2 3 C 30
3 2 B 40
bill024 2007-06-26
  • 打赏
  • 举报
回复
create table tabel1(id int,name varchar(10),money int)
insert tabel1 select 1,'A',20
union all select 2,'B',40
union all select 3,'C',30

select 排名=(select count(1)+1 from tabel1 where money<a.money),id,name,money from tabel1 a
order by 排名,money desc

drop table tabel1
GEPIN 2007-06-26
  • 打赏
  • 举报
回复
怎么没明白你的意思呢?

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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