sqlserver怎么实现分组排名? SELECT * FROM tb a WHERE 3>(SELECT COUNT(1) FROM tb b WHERE a.sortid=b.sortid AND b.price<a.price)随意手写,未经测试。假设你表名为tb 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 select * from 表名 a where sortid in (select top 3 price from 表名 where sortid=a.sortid) itblog(BegCSharp) 反了,要倒序的price^^ declare @t table(id int,sortid varchar(10),price decimal(10,1))insert @t select 1, '001', 2.2insert @t select 2, '002', 2.5insert @t select 3, '001', 1.8insert @t select 4, '001', 1.3insert @t select 5, '002', 2.6insert @t select 6, '003', 2.8insert @t select 7, '002', 1.9insert @t select 8, '003', 1.5insert @t select 9, '002', 1.75insert @t select 10, '003', 1.35insert @t select 11, '003', 1.45insert @t select 12, '001', 1.5select distinct sortid,price from @t a where price in (select top 3 price from @t where sortid=a.sortid) 更正一下之前的那句:select distinct sortid,price from 表名 a where price in (select top 3 price from 表名 where sortid=a.sortid) 这种写法从语句上看好像是取同一sortid的top 3 price这没错,但我老觉得逻辑有问题,可又说不出具体在哪.. declare @t table(id int,sortid varchar(10),price decimal(10,1))insert @t select 1, '001', 2.2insert @t select 2, '002', 2.5insert @t select 3, '001', 1.8insert @t select 4, '001', 1.3insert @t select 5, '002', 2.6insert @t select 6, '003', 2.8insert @t select 7, '002', 1.9insert @t select 8, '003', 1.5insert @t select 9, '002', 1.75insert @t select 10, '003', 1.35insert @t select 11, '003', 1.45insert @t select 12, '001', 1.5select * from @t t where(select count(*) from @t where sortid=t.sortid and price<t.price)<3 select * from @t t where id in(select top 3 id from @t where sortid=t.sortid order by price) 谢谢大家的帮忙!我测试一下,fcuandy(要学的东西还很多) 和lxzm1001(*蓝星之梦*) 的方法都是正确的。lxzm1001(*蓝星之梦*) 的第二种方法也可以,但可能没有考虑到名次重复的问题。itblog(BegCSharp) 的方法执行的结果不对,语句可能有点问题,但也给分了。 请大家帮忙看下数据库的问题已经2天了,还没能解决 请问如何在金蝶查询分析器里增加字段显示颜色 sql查询语句错误 请高手帮忙 求一个过滤html标签的存储过程 恢复数据库,用户无法恢复 关于即席查询的问题。》》》》》》》》》 使用 restore filelistonly 命令查阅逻辑文件名,在编程中如何获得? 如何把一个表中的所有记录快速复制到另外一个表中 一个少见的问题,请高手进入!!修改表结构时,怎么样触发另外一事件?在线等待!! 98上装sql的Desktop版,也有复制的功能项,不知能不能成功?有谁试过吗? 还是写SQL语句的难题 如何从一个表中选出数据,生成所需另一表格
insert @t select 1, '001', 2.2
insert @t select 2, '002', 2.5
insert @t select 3, '001', 1.8
insert @t select 4, '001', 1.3
insert @t select 5, '002', 2.6
insert @t select 6, '003', 2.8
insert @t select 7, '002', 1.9
insert @t select 8, '003', 1.5
insert @t select 9, '002', 1.75
insert @t select 10, '003', 1.35
insert @t select 11, '003', 1.45
insert @t select 12, '001', 1.5
select distinct sortid,price from @t a where price in (select top 3 price from @t where sortid=a.sortid)
insert @t select 1, '001', 2.2
insert @t select 2, '002', 2.5
insert @t select 3, '001', 1.8
insert @t select 4, '001', 1.3
insert @t select 5, '002', 2.6
insert @t select 6, '003', 2.8
insert @t select 7, '002', 1.9
insert @t select 8, '003', 1.5
insert @t select 9, '002', 1.75
insert @t select 10, '003', 1.35
insert @t select 11, '003', 1.45
insert @t select 12, '001', 1.5select * from @t t where(select count(*) from @t where sortid=t.sortid and price<t.price)<3