Saturday, 6 May 2017

sql - Postgres SELECT DISTINCT, only highest values




I am using the following pgsql query to select index_id and making a distinct query on the level column.



SELECT DISTINCT ON (level) index_id FROM indexes 


Although its working, it is returning the first index_id for each level, i would like to get the highest index_id in each distinct level


Answer



Use order by to get the max index_id




SELECT DISTINCT ON (level) index_id 
FROM indexes
order by level, index_id desc

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...