Antelope
Antelope

Reputation: 31

how to get rows which have max value2 when value1 is equal using django queryset

for example

rowid    value1   value2
1        1         2
2        1         3
3        2         4

then select (1,1,3) and (3,2,4) because row1 and row2 have the same value1, and value2 of row2 is bigger than value2 of row1

thank you!

Upvotes: 0

Views: 40

Answers (1)

SqlZim
SqlZim

Reputation: 38023

In sql this is a simple aggregation query using group by and min()/max()

select 
    min(rowid) as rowid
  , value1
  , max(value2) as value2
from t
group by value1

Upvotes: 2

Related Questions