1

My table

integer   | party   | value | and many more columns
----------+---------+-------+----------------------
 1        | abc     |  40   | -----
 1        | abc     |  90   | -----
 2        | pqr     |  12   | -----
 1        | abc     | 120   | -------
 2        | pqr     |  86   | --------

and so on

I want entire row to be retrieved which has max value

Sample output for above table

1    abc    120
2    pqr     86

For a particular distinct party I want row to be picked up which has maximum value.

Mureinik
  • 297,002
  • 52
  • 306
  • 350

2 Answers2

1

You can use the rank window function to find the "max" row per party:

SELECT 
    id, party, value
FROM
    (SELECT 
         id, party, value, 
         RANK() OVER (PARTITION BY party ORDER BY value DESC) AS rk
     FROM   
         mytable) t
WHERE  
    rk = 1
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

use simple aggregation function MAX(), assume integer is a id column

select id,party, max(value) as max_val from your_table
group by id,party
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63