[英]Select rows with Max(Column Value) for each unique combination of two other columns
I'm working with a sample table like below.我正在使用如下示例表。 A Dataset has multiple groups, and each time a write to the table occurs, the RunNumber increments for the dataset, along with data for each group and the total.
一个数据集有多个组,每次写入表时,数据集的 RunNumber 以及每个组的数据和总数都会增加。 Each Dataset/Group combo will usually have multiple rows, example below:
每个数据集/组组合通常会有多行,示例如下:
RunNumber![]() |
Group![]() |
Dataset![]() |
Total![]() |
---|---|---|---|
1 ![]() |
Group1![]() |
Dataset A![]() |
10 ![]() |
1 ![]() |
Group1![]() |
Dataset A![]() |
20 ![]() |
2 ![]() |
Group1![]() |
Dataset A![]() |
30 ![]() |
2 ![]() |
Group2![]() |
Dataset A![]() |
15 ![]() |
1 ![]() |
Group1![]() |
Dataset B![]() |
5 ![]() |
1 ![]() |
Group2![]() |
Dataset B![]() |
10 ![]() |
1 ![]() |
Group3![]() |
Dataset A![]() |
30 ![]() |
2 ![]() |
Group3![]() |
Dataset A![]() |
30 ![]() |
1 ![]() |
Group1![]() |
Dataset C![]() |
15 ![]() |
1 ![]() |
Group2![]() |
Dataset C![]() |
50 ![]() |
2 ![]() |
Group2![]() |
Dataset C![]() |
70 ![]() |
2 ![]() |
Group2![]() |
Dataset C![]() |
90 ![]() |
What I want to do is essential for each combination of Dataset and Group, return all data for rows that have the max(RunNumber) for the given Dataset/Group combination.我想要做的对于数据集和组的每个组合都是必不可少的,返回具有给定数据集/组组合的 max(RunNumber) 的行的所有数据。 So for example, the above sample would return this:
例如,上面的示例将返回:
RunNumber![]() |
Group![]() |
Dataset![]() |
Total![]() |
---|---|---|---|
2 ![]() |
Group1![]() |
Dataset A![]() |
30 ![]() |
2 ![]() |
Group2![]() |
Dataset A![]() |
15 ![]() |
1 ![]() |
Group1![]() |
Dataset B![]() |
5 ![]() |
1 ![]() |
Group2![]() |
Dataset B![]() |
10 ![]() |
2 ![]() |
Group3![]() |
Dataset A![]() |
30 ![]() |
1 ![]() |
Group1![]() |
Dataset C![]() |
15 ![]() |
2 ![]() |
Group2![]() |
Dataset C![]() |
70 ![]() |
2 ![]() |
Group2![]() |
Dataset C![]() |
90 ![]() |
Where the Dataset/Groups match, all rows are kept with the max RunNumber for that given combo.在数据集/组匹配的地方,所有行都保留给定组合的最大 RunNumber。 For now, I've split this into 2 separate queries, where i first query for the max(RunNumber) for all distinct Dataset/Group combos, then do a select * for all matches.
目前,我已将其拆分为 2 个单独的查询,其中我首先查询所有不同数据集/组组合的 max(RunNumber),然后对所有匹配项执行 select *。 Any help would be appreciated, thanks in advance!
任何帮助将不胜感激,在此先感谢!
In MySQL 5.x you can use a sub-query.在 MySQL 5.x 中,您可以使用子查询。
SELECT *
FROM your_table
WHERE (`Group`, Dataset, RunNumber) IN (
SELECT `Group`, Dataset, MAX(RunNumber) AS MaxRunNumber
FROM your_table
GROUP BY `Group`, Dataset
);
Test on db<>fiddle here在这里测试 db<>fiddle
Alternatives备择方案
--
-- LEFT JOIN on bigger
--
SELECT t.*
FROM your_table t
LEFT JOIN your_table t2
ON t2.`Group` = t.`Group`
AND t2.Dataset = t.Dataset
AND t2.RunNumber > t.RunNumber
WHERE t2.RunNumber IS NULL
ORDER BY t.`Group`, t.Dataset;
--
-- where NOT EXISTS on bigger
--
SELECT *
FROM your_table t
WHERE NOT EXISTS (
SELECT 1
FROM your_table t2
WHERE t2.`Group` = t.`Group`
AND t2.Dataset = t.Dataset
AND t2.RunNumber > t.RunNumber
)
ORDER BY `Group`, Dataset;
--
-- DENSE_RANK = 1
-- MySql 8 and beyond.
--
SELECT *
FROM
(
SELECT *
, DENSE_RANK() OVER (PARTITION BY `Group`, Dataset ORDER BY RunNumber DESC) AS rnk
FROM your_table
) q
WHERE rnk = 1
ORDER BY `Group`, Dataset;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.