一个计算排名的问题,也许高手能实现,也许实现不了...欢迎讨论!
根据一个数据集,生成一个带排名的数据集!
虚拟一个元表pro
product | type | kind |fee
a big ttt 1000
a mid ccc 5465
b lit bbb 5644
b mid ddd 4678
c big ddd 7987
c lit fff 5445
我从一张表中统计出一个数据集(不是表)如下:
product | fee
a 6465.00
b 10322.00
c 13432.00
我想直接从这个数据集中统计出一个排名列如下:
product | fee | ord
a 6465.00 3
b 10322.00 2
c 13432.00 1
使用数据集直接生成,不使用表!我用不同的方法试了几次都不行!有没有牛人?
问题点数:100、回复次数:7Top
1 楼aw511(点点星灯)回复于 2005-07-18 15:40:15 得分 30
--参考我下面的这个,你看看是否是你想要的
declare @chengji table(xuehao varchar(20),xingming varchar(50),zongfen int)
insert @chengji(xuehao,xingming,zongfen)
values('0010','lixue',500)
insert @chengji(xuehao,xingming,zongfen)
values('0015','zhansi',456)
insert @chengji(xuehao,xingming,zongfen)
values('0014','heyao',512)
insert @chengji(xuehao,xingming,zongfen)
values('0016','woshi',423)
select * from @chengji
declare @newchengji table(mingci int,xuehao varchar(20),xingming varchar(50),zongfen int)
insert @newchengji
select 1+isnull((select count(*) from @chengji where zongfen>a.zongfen),0)
as mingci,xuehao,xingming,zongfen from @chengji a
order by zongfen desc
select * from @newchengjiTop
2 楼filebat(Mark)回复于 2005-07-18 15:40:56 得分 5
呵呵,可以做到(通过子查询, 但是效率可能不高)
如果fee相同时, 如何处理(即是否允许并列情况?)Top
3 楼vivianfdlpw()回复于 2005-07-18 15:40:57 得分 5
select product,fee ,[ord]=(select count(1) from 数据集 where fee<=A.fee)
from 数据集 ATop
4 楼aw511(点点星灯)回复于 2005-07-18 15:41:30 得分 5
mingci xuehao xingming zongfen
----------- -------------------- -------------------------------------------------- -----------
1 0014 heyao 512
2 0010 lixue 500
3 0015 zhansi 456
4 0016 woshi 423
------------------------------------------------------
mingci这一列就你想要的把Top
5 楼filebat(Mark)回复于 2005-07-18 15:48:12 得分 40
--测试数据
create table ta(product varchar(10), type varchar(10),
kind varchar(10), fee real)
insert ta select 'a', 'big', 'ttt', 1000
union all select 'a', 'mid', 'ccc', 5465
union all select 'b', 'lit', 'bbb', 5644
union all select 'b', 'mid', 'ddd', 4678
union all select 'c', 'big', 'ddd', 7987
union all select 'c', 'lit', 'fff', 5445
--查询
select *, id=(select 1+isnull(count(*), 0)
from(select product, fee=sum(fee) from ta group by product)tt
where tt.fee>t.fee)
from(select product, fee=sum(fee) from ta group by product)t
--清除
drop table ta
Top
6 楼MorningTea(一勺抹茶)回复于 2005-07-18 16:28:06 得分 10
方法一:
select identity(int,1,1) as ord,p.product,t.fee into #tt from pro p inner join (select * from 表)t --(select * from 表)里面是你统计数据集的语句
on p.product = t.product
select #tt.product,#tt.fee,#tt.sn from #tt order by sn desc
方法二:
select p.product,t.fee,ord=(select count(*) from (select * from 表) t1 where t1.fee <= t.fee)
from pro p inner join (select * from 表)t
on p.product = t.productTop
7 楼MorningTea(一勺抹茶)回复于 2005-07-18 16:29:43 得分 5
方法二:
select p.product,t.fee,ord=(select count(*) from (select * from 表) t1 where t1.fee <= t.fee)
from pro p inner join (select * from 表)t
on p.product = t.product
order by t.fee desc ---加上这句
Top




