读书人

这个sql如何写

发布时间: 2012-01-14 20:02:35 作者: rapoo

这个sql怎么写
简单的说 一个表 根据id将数量 group by以后 我想得到数量的排名

比如
id 数量 排名
1 100 1
2 50 2

[解决办法]

create table #t(id int,数量 int)

insert into #t select 1,100
insert into #t select 2,50


select * from #t

--用子查询
select *,(select count(*) from #t where 数量> =A.数量) as 名次
from #t as a
order by id

drop table #t

[解决办法]

create table #t(id int,数量 int)

insert into #t select 1,100
insert into #t select 2,50


select * from #t

--用子查询
select *,(select count(*)+1 from #t where 数量> A.数量) as 名次
from #t as a
order by id

drop table #t
[解决办法]
select
id,
数量,
排名=(select
count(1)
from
table
where
数量> a.数量)+1
from
table a
[解决办法]
select identity(int,1),* into #AA from AA order by 数量 desc
select * from #AA
drop table #AA

读书人网 >SQL Server

热点推荐