简体   繁体   English

选择具有最大列值的行按另一列选择

[英]Select Rows with Maximum Column Value group by Another Column

This should be a simple question, but I can't get it to work :( 这应该是一个简单的问题,但我不能让它工作:(

How to select rows that have the maximum column value,as group by another column? 如何选择具有最大列值的行,作为另一列的组?

For example, 例如,

I have the following table definition: 我有以下表定义:

ID
Del_Index
docgroupviewid

The issue now is that I want to group by results by docgroupviewid first, and then choose one row from each docgroupviewid group, depending on which one has the highest del_index . 现在的问题是我想docgroupviewid按结果docgroupviewid ,然后从每个docgroupviewid组中选择一行,具体取决于哪一行具有最高的del_index

I tried 我试过了

SELECT docgroupviewid, max(del_index),id FROM table
group by docgroupviewid

But instead of return me with the correct id , it returns me with the earliest id from the group with the same docgroupviewid . 但是不是使用正确的id返回给我,而是返回具有相同docgroupviewid的组中最早的id

Any ideas? 有任何想法吗?

I've struggled with this many times myself and the solution is to think about your query differently. 我自己多次努力,解决方案是以不同的方式思考你的查询。

I want each DocGroupViewID row where the Del_Index is the highest(max) for all rows with that DocGroupViewID: 我想要每个DocGroupViewID行,其中Del_Index是具有该DocGroupViewID的所有行的最高(最大):

SELECT
    T.DocGroupViewID,
    T.Del_Index,
    T.ID
FROM MyTable T
WHERE T.Del_Index = (
    SELECT MAX( T1.Del_Index ) FROM MyTable T1
    WHERE T1.DocGroupViewID = T.DocGroupViewID 
)

It gets more complex when more than one row can have the same Del_Index , since then you need some way to choose which one to show. 当多个行可以具有相同的Del_Index ,它会变得更复杂,因为那时您需要某种方式来选择要显示的行。


EDIT: wanted to follow up with another option 编辑:想跟进另一个选项

You can use the RANK() or ROW_NUMBER() functions with a CTE to get more control over the results, as follows: 您可以将RANK()ROW_NUMBER()函数与CTE一起使用,以获得对结果的更多控制,如下所示:

-- fake a source table
DECLARE @t TABLE (
    ID int IDENTITY(1,1) PRIMARY KEY,
    Del_Index int,
    DocGroupViewID int
)

INSERT INTO @t
SELECT 1, 1 UNION ALL
SELECT 2, 1 UNION ALL
SELECT 3, 1 UNION ALL
SELECT 1, 2 UNION ALL
SELECT 2, 2 UNION ALL
SELECT 2, 2 UNION ALL
SELECT 1, 3 UNION ALL
SELECT 2, 3 UNION ALL
SELECT 3, 3 UNION ALL
SELECT 4, 3

-- show our source
SELECT * FROM @t

-- select using RANK (can have duplicates)
;WITH cteRank AS
(
    SELECT
        DocGroupViewID,
        Del_Index,
        ID,
        RANK() OVER
            (PARTITION BY DocGroupViewID ORDER BY Del_Index DESC)
        AS RowRank,
        ROW_NUMBER() OVER
            (PARTITION BY DocGroupViewID ORDER BY Del_Index DESC)
        AS RowNumber
    FROM @t
)
SELECT *
FROM cteRank
WHERE RowRank = 1

-- select using ROW_NUMBER
;WITH cteRowNumber AS
(
    SELECT
        DocGroupViewID,
        Del_Index,
        ID,
        RANK() OVER
            (PARTITION BY DocGroupViewID ORDER BY Del_Index DESC)
        AS RowRank,
        ROW_NUMBER() OVER
            (PARTITION BY DocGroupViewID ORDER BY Del_Index DESC)
        AS RowNumber
    FROM @t
)
SELECT *
FROM cteRowNumber
WHERE RowNumber = 1

If you have ways to sort out ties, just add it to the ORDER BY . 如果您有办法解决关系,只需将其添加到ORDER BY

You will have to complicate your query a little bit: 您将不得不稍微复杂一下您的查询:

select a.docgroupviewid, a.del_index, a.id from table a
    where a.del_index = (select max(b.del_index) from table
        where b.docgroupviewid = a.docgroupviewid)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL-选择列值仅为0的行,按另一列分组? - MySQL - Select rows where column value is only 0, group by another column? SQL选择列,另一列中的最大值 - SQL Select column with maximum value in another column Select 列基于另一列中的最大值 - Select Column Based on Maximum Value in Another Column 选择具有最大列值的行由另一列分组,没有嵌套的选择语句 - Select Rows with maximum column value grouped by another column without nested select statement 如何选择一列具有最大值的行 - How to select rows that have a maximum value for a column Mysql 按列分组,只取第三列最大值的行 - Mysql group by a column and get the rows only with the maximum value of the third column Select 行使用 group by 并且在每个组中获取基于另一个列值的最高值的列值 - Select rows using group by and in each group get column values based on highest of another column value 选择该列中特定列值为最大值的所有行 - Select all rows where specific column value is maximum of that column 按列分组并在另一列中获取具有最大字符串长度的行 - Group by columns and fetch rows with maximum length of string in another column mysql如何选择列的最大值作为结果集中的另一列 - mysql how to select maximum value of a column as another column in the resultset
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM