从每种类型中选择3行 [英] Select 3 rows from each type

查看:36
本文介绍了从每种类型中选择3行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的表中有2列,ID和报告。



报告有5个类别:台式机,笔记本电脑,服务器,显示器和整体



每个类别有5行。



我需要每个类别的前三个ID。



请帮助。



提前致谢。

解决方案

您好,请看这个链接:



如何选择每组的前N行 [ ^ ]

为每个组选择前N行表 [ ^ ]



希望它有所帮助! :)


可以通过这个来实现。





 ID为 as  
(SELECT id,
report_type,ROW_NUMBER()OVER(PARTITION BY report_type ORDER BY id DESC)
as ' RowNum' 来自 tablename)
SELECT id,report_type FROM ID WHERE RowNum < = 3


Hi,

I have 2 columns in my table, ID and Report.

Report has 5 categories: Desktop, Laptop, Server, Monitor and Overall

Each category has 5 rows.

I need top three IDs from each category.

Please help.

Thanks in advance.

解决方案

Hi, See this link:

How to select first N row of each group[^]
Selecting top N rows for each group in a table[^]

Hope it helps! :)


It can be achieved by this.


with ID as 
(SELECT id, 
report_type, ROW_NUMBER() OVER(PARTITION BY report_type ORDER BY id DESC)
 as 'RowNum' from tablename)
SELECT id,report_type FROM ID WHERE RowNum <= 3


这篇关于从每种类型中选择3行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆