日期:2014-05-18  浏览次数:20449 次

这个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