请教一个有关分组查询的问题

fwilyair 2013-10-16 10:24:31
我现在需要做一个查询 就是把每天销售业绩前五名的手机抓取出来
再抓取出这5个品牌的最高销量的型号
但是现在有一个问题 就是比如第一名A品牌 但是按照销量来排序
他可能有好几个型号都在榜上 所以如果TOP5的话就会有重复的品牌 导致抓不到其他的品牌了
不知道我这么说可以理解么
比如销量前五的分别是
A
B
C
D
E
我需要的就是抓取出对应的型号 比如"
A AA-1
B BB-1
C CC-1
D DD-1
E EE-1
但是现在有可能是这样的

A AA-1
A AA-2
B BB-1
C CC-1
C CC-2

这样的话我就抓不到D和E的数据了

求指导= =
...全文
90 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dengixnyu 2013-10-16
  • 打赏
  • 举报
回复
就是分组之后,求每组的第一条吧 不要用top,查询的时候加个标识列。row_number() over(partition by 型号 order by 数量) 最后加where 标识列= 1 就好了
IEEE_China 2013-10-16
  • 打赏
  • 举报
回复

--创建临时表
if object_id('Tempdb..#t') is not null drop table #t
create table #t(
id int identity(1,1) not null,
brand nvarchar(100) null,
model nvarchar(100) null,
sales int null
)
--插入数据
Insert Into #t
select 'A','A1',101 union all
select 'A','A2',122 union all
select 'A','A3',113 union all
select 'A','A4',104 union all
select 'A','A5',95 union all
select 'B','B1',121 union all
select 'B','B2',112 union all
select 'B','B3',103 union all
select 'B','B4',131 union all
select 'C','C1',112 union all
select 'C','C2',122 union all
select 'C','C3',103 union all
select 'C','C4',124 union all
select 'D','D1',133 union all
select 'D','D2',131 union all 
select 'D','D3',135 union all
select 'E','E1',132 union all
select 'E','E2',133
--查询
select top 5 *, row_number() over(partition by brand order by sales desc) as orderid 
from #t
order by orderid,sales desc
-------------------------
--查询结果
(18 行受影响)
id          brand                                                                                                model                                                                                                sales       orderid
----------- ---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- ----------- --------------------
16          D                                                                                                    D3                                                                                                   135         1
18          E                                                                                                    E2                                                                                                   133         1
9           B                                                                                                    B4                                                                                                   131         1
13          C                                                                                                    C4                                                                                                   124         1
2           A                                                                                                    A2                                                                                                   122         1

(5 行受影响)

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧