gpt4 book ai didi

mysql - 获取每组分组结果的前n条记录

转载 作者:行者123 更新时间:2023-11-29 15:12:55 25 4
gpt4 key购买 nike

以下是最简单的示例,但任何解决方案都应该能够扩展到所需的 n 个顶级结果:

给定一个如下表,其中包含人员、组和年龄列,您如何获得每个组中年龄最大的 2 个人?(组内的关系不应产生更多结果,但会给出前 2 个按字母顺序排列)

+--------+-------+-----+| Person | Group | Age |+--------+-------+-----+| Bob    | 1     | 32  || Jill   | 1     | 34  || Shawn  | 1     | 42  || Jake   | 2     | 29  || Paul   | 2     | 36  || Laura  | 2     | 39  |+--------+-------+-----+

Desired result set:

+--------+-------+-----+| Shawn  | 1     | 42  || Jill   | 1     | 34  || Laura  | 2     | 39  || Paul   | 2     | 36  |+--------+-------+-----+

NOTE: This question builds on a previous one- Get records with max value for each group of grouped SQL results - for getting a single top row from each group, and which received a great MySQL-specific answer from @Bohemian:

select * 
from (select * from mytable order by `Group`, Age desc, Person) x
group by `Group`

希望能够以此为基础,尽管我不知道如何实现。

最佳答案

这是执行此操作的一种方法,即使用 UNION ALL (请参阅 SQL Fiddle with Demo )。这适用于两个组,如果您有两个以上的组,则需要指定编号并为每个添加查询:

(
select *
from mytable
where `group` = 1
order by age desc
LIMIT 2
)
UNION ALL
(
select *
from mytable
where `group` = 2
order by age desc
LIMIT 2
)

有多种方法可以执行此操作,请参阅本文以确定适合您情况的最佳路线:

http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/

编辑:

这可能也适合您,它为每条记录生成一个行号。使用上面链接中的示例,这将仅返回行号小于或等于 2 的记录:

select person, `group`, age
from
(
select person, `group`, age,
(@num:=if(@group = `group`, @num +1, if(@group := `group`, 1, 1))) row_number
from test t
CROSS JOIN (select @num:=0, @group:=null) c
order by `Group`, Age desc, person
) as x
where x.row_number <= 2;

参见Demo

关于mysql - 获取每组分组结果的前n条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59930202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com