Select max value of each group

Sql

Sql Problem Overview


Name  	Value  	AnotherColumn
-----------
Pump 1	8000.0	Something1
Pump 1	10000.0	Something2
Pump 1	10000.0	Something3
Pump 2	3043	Something4
Pump 2	4594	Something5
Pump 2	6165	Something6

My table looks something like this. I would like to know how to select max value for each pump.

select a.name, value from out_pumptable as a,
(select name, max(value) as value from out_pumptable where group by posnumber)g where and g.value = value

this code does the job, but i get two entries of Pump 1 since it has two entries with same value.

Sql Solutions


Solution 1 - Sql

select name, max(value)
from out_pumptable
group by name

Solution 2 - Sql

select name, value 
from( select name, value, ROW_NUMBER() OVER(PARTITION BY name ORDER BY value desc) as rn
from out_pumptable ) as a
where rn = 1

Solution 3 - Sql

SELECT
  b.name,
  MAX(b.value) as MaxValue,
  MAX(b.Anothercolumn) as AnotherColumn
FROM out_pumptabl
INNER JOIN (SELECT 
              name,
              MAX(value) as MaxValue
            FROM out_pumptabl
            GROUP BY Name) a ON 
  a.name = b.name AND a.maxValue = b.value
GROUP BY b.Name

Note this would be far easier if you had a primary key. Here is an Example

SELECT * FROM out_pumptabl c
WHERE PK in 
    (SELECT
      MAX(PK) as MaxPK
    FROM out_pumptabl b
    INNER JOIN (SELECT 
                  name,
                  MAX(value) as MaxValue
                FROM out_pumptabl
                GROUP BY Name) a ON 
      a.name = b.name AND a.maxValue = b.value) 

Solution 4 - Sql

select Name, Value, AnotherColumn
from out_pumptable
where Value =
(
  select Max(Value)
  from out_pumptable as f where f.Name=out_pumptable.Name
)
group by Name, Value, AnotherColumn

Try like this, It works.

Solution 5 - Sql

select * from (select * from table order by value desc limit 999999999) v group by v.name

Solution 6 - Sql

Using analytic function is the easy way to find max value of every group.

> Documentation : https://docs.microsoft.com/en-us/sql/t-sql/functions/row-number-transact-sql?view=sql-server-ver15

Select name, 
       value,
       AnotherColumn
From(
    SELECT Row_Number() over(partition by name order by value desc)as 
   row_number, *
FROM students
) 
 Where row_number = 1


 

Solution 7 - Sql

SELECT t1.name, t1.Value, t1.AnotherColumn
FROM mytable t1
JOIN (SELECT name AS nameMax, MAX(Value) as valueMax 
      FROM mytable 
      GROUP BY name) AS t2 
    ON t2.nameMax = t1.name AND t2.valueMax = t1.Value
WHERE 1 OR <anything you would like>
 GROUP BY t1.name;

Solution 8 - Sql

SELECT DISTINCT (t1.ProdId), t1.Quantity FROM Dummy t1 INNER JOIN
       (SELECT ProdId, MAX(Quantity) as MaxQuantity FROM Dummy GROUP BY ProdId) t2
    ON t1.ProdId = t2.ProdId
   AND t1.Quantity = t2.MaxQuantity
 ORDER BY t1.ProdId

this will give you the idea.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWai WongView Question on Stackoverflow
Solution 1 - Sqlm.edmondsonView Answer on Stackoverflow
Solution 2 - Sqltwk7890View Answer on Stackoverflow
Solution 3 - SqlJohn HartsockView Answer on Stackoverflow
Solution 4 - SqlLilit GalstyanView Answer on Stackoverflow
Solution 5 - SqlUmair SheikhView Answer on Stackoverflow
Solution 6 - SqlShuchita RahamnView Answer on Stackoverflow
Solution 7 - SqlFabien HaddadiView Answer on Stackoverflow
Solution 8 - SqlMuhammad JahanzebView Answer on Stackoverflow