社区
疑难问题
帖子详情
sqlserver怎么实现分组排名?
bfly2000
2006-06-29 05:39:08
表结构是这样的:
id sortid price
1 001 2.2
2 002 2.5
3 001 1.8
4 001 1.3
5 002 2.6
6 003 2.8
7 002 1.9
8 003 1.5
9 002 1.75
10 003 1.35
11 003 1.45
12 001 1.5
请问现在如何查询出每一类sortid中price最小的前三名
...全文
451
13
打赏
收藏
sqlserver怎么实现分组排名?
表结构是这样的: idsortidprice 10012.2 20022.5 30011.8 40011.3 50022.6 60032.8 70021.9 80031.5 90021.75 100031.35 110031.45 120011.5 请问现在如何查询出每一类sortid中price最小的前三名
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
13 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
bfly2000
2006-06-30
打赏
举报
回复
谢谢大家的帮忙!我测试一下,fcuandy(要学的东西还很多) 和lxzm1001(*蓝星之梦*) 的方法都是正确的。lxzm1001(*蓝星之梦*) 的第二种方法也可以,但可能没有考虑到名次重复的问题。itblog(BegCSharp) 的方法执行的结果不对,语句可能有点问题,但也给分了。
liangpei2008
2006-06-29
打赏
举报
回复
学习...
lxzm1001
2006-06-29
打赏
举报
回复
select * from @t t where id in(select top 3 id from @t where sortid=t.sortid order by price)
lxzm1001
2006-06-29
打赏
举报
回复
declare @t table(id int,sortid varchar(10),price decimal(10,1))
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 * from @t t where(select count(*) from @t where sortid=t.sortid and price<t.price)<3
fcuandy
2006-06-29
打赏
举报
回复
这种写法从语句上看好像是取同一sortid的top 3 price这没错,但我老觉得逻辑有问题,可又说不出具体在哪..
itblog
2006-06-29
打赏
举报
回复
更正一下之前的那句:
select distinct sortid,price from 表名 a where price in (select top 3 price from 表名 where sortid=a.sortid)
itblog
2006-06-29
打赏
举报
回复
declare @t table(id int,sortid varchar(10),price decimal(10,1))
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)
LouisXIV
2006-06-29
打赏
举报
回复
搞错^^;
LouisXIV
2006-06-29
打赏
举报
回复
itblog(BegCSharp)
反了,要倒序的price^^
marco08
2006-06-29
打赏
举报
回复
学习...
itblog
2006-06-29
打赏
举报
回复
select * from 表名 a where sortid in (select top 3 price from 表名 where sortid=a.sortid)
LouisXIV
2006-06-29
打赏
举报
回复
:)
fcuandy
2006-06-29
打赏
举报
回复
SELECT * FROM tb a
WHERE 3>(SELECT COUNT(1) FROM tb b WHERE a.sortid=b.sortid AND b.price<a.price)
随意手写,未经测试。假设你表名为tb
[电子书] SQL Server 2012 T-SQL 实用技巧 (英文版)
《SQL Server 2012 T-SQL 实用技巧》是一本专为SQL Server 2012数据库管理员和开发人员编写的指南,由Apress出版社出版。这本书以英文呈现,旨在提供解决日常T-SQL编程挑战的具体解决方案,涵盖了T-SQL语言的广泛...
SQL 关于记录排序
sqlserver
1. **
分组
排名
**:当需要对每个组内的数据进行
排名
时,可以结合 `GROUP BY` 使用。 2. **复杂更新逻辑**:结合 `CASE` 语句和其他逻辑表达式,可以
实现
更为复杂的更新规则。 3. **数据聚合**:与 `COUNT`, `SUM`, `...
《Microsoft SQL Server 2005技术内幕:T-SQL查询》示例代码
《Microsoft SQL Server 2005技术内幕:T-SQL查询》是一本深入解析SQL Server 2005中T-SQL查询语言的专业书籍。T-SQL(Transact-SQL)是微软对标准SQL的扩展,是SQL Server的核心组件,用于数据库管理、数据查询和...
学生管理系统(VC++ SQL SERVER)
本项目采用的是经典的VC++编程语言与SQL SERVER数据库的结合,
实现
了高效、稳定且功能丰富的学生信息管理平台。 VC++,全称为Microsoft Visual C++,是一款强大的Windows应用程序开发工具。它提供了集成开发环境,...
Microsoft SQL Server 2008技术内幕
《Microsoft SQL Server 2008技术内幕》是一本深入探讨微软SQL Server 2008核心技术的专业书籍,旨在帮助读者全面理解并熟练掌握这一数据库管理系统的关键特性。该书聚焦于T-SQL查询,这是SQL Server中进行数据检索...
疑难问题
22,302
社区成员
121,734
社区内容
发帖
与我相关
我的任务
疑难问题
MS-SQL Server 疑难问题
复制链接
扫一扫
分享
社区描述
MS-SQL Server 疑难问题
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章