[英]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
,它会变得更复杂,因为那时您需要某种方式来选择要显示的行。
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.