Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get records with max value from two columns in postgresql

I have table structure like below in postgressql
Where one ticketid can have multiple priority and rating

If i want the list of all ticket ids who have max priority as 2 and max rating as 3.
Then output should give t2 and t4

If i want the list of all ticket ids who have max priority as 3 and max rating as 3.
Then output should give t1 only

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am not sure how to write a query for this

enter image description here

>Solution :

You can use the HAVING keyword to filter an aggregate after grouping:

SELECT ticketid 
FROM ratings 
GROUP BY ticketid 
HAVING max(priority) = 2 
AND max(rating) = 3;
 ticketid
----------
 t4
 t2
(2 rows)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading