简体   繁体   English

Select 行与其他两列的每个唯一组合的 Max(列值)

[英]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 1 Group1第 1 组 Dataset A数据集 A 10 10
1 1 Group1第 1 组 Dataset A数据集 A 20 20
2 2 Group1第 1 组 Dataset A数据集 A 30 30
2 2 Group2第 2 组 Dataset A数据集 A 15 15
1 1 Group1第 1 组 Dataset B数据集 B 5 5
1 1 Group2第 2 组 Dataset B数据集 B 10 10
1 1 Group3第 3 组 Dataset A数据集 A 30 30
2 2 Group3第 3 组 Dataset A数据集 A 30 30
1 1 Group1第 1 组 Dataset C数据集 C 15 15
1 1 Group2第 2 组 Dataset C数据集 C 50 50
2 2 Group2第 2 组 Dataset C数据集 C 70 70
2 2 Group2第 2 组 Dataset C数据集 C 90 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 2 Group1第 1 组 Dataset A数据集 A 30 30
2 2 Group2第 2 组 Dataset A数据集 A 15 15
1 1 Group1第 1 组 Dataset B数据集 B 5 5
1 1 Group2第 2 组 Dataset B数据集 B 10 10
2 2 Group3第 3 组 Dataset A数据集 A 30 30
1 1 Group1第 1 组 Dataset C数据集 C 15 15
2 2 Group2第 2 组 Dataset C数据集 C 70 70
2 2 Group2第 2 组 Dataset C数据集 C 90 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.

相关问题 如何根据一列的最大值选择MySQL中的行并将另外两列分组? - How to select rows from MySQL based on max value of a one column and grouping two other columns? MySQL:如何在给定其他某个列值的情况下选择其中两个列值的组合唯一的行 - Mysql: how to select rows where the combination of 2 of the column values are unique given a certain other column value MySQL选择两列中唯一的行,其中一列中的值最高 - MySQL select unique rows in two columns with the highest value in one column 选择两列的唯一组合 - Select a unique combination of two columns 如何查询mysql以选择具有两列非唯一组合的行 - How to query mysql to select rows with non unique combination of two columns 通过两列的组合选择不同的项目,其中第三列的值为max- LINQ - Select distinct items by combination of two columns and where Value of third column is max- LINQ 选择具有按两列分组的最大值的行 - Select rows with Max Value grouped by two columns 获取具有两列组合的唯一行 - Get unique rows with combination of two columns 获取两列的每个唯一组合及其最大值和最小值的完整数据集 - Get whole dataset for each unique combination of two columns and their max and min values 选择两列的最大值并获取每一行的ID - Select max value for two columns and get id of each row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM