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

Postgresql Get Maximum value per day with corresponding time

I have the following table:

Date      | Time     | Value  | ReceivedTime
2022-04-01| 00:59:59 | 5      | 00:30:15
2022-04-01| 13:59:59 | 15     | 13:30:00
2022-04-02| 21:59:59 | 5      | 21:30:15
2022-04-02| 22:59:59 | 25     | 22:25:15
2022-04-02| 23:59:59 | 25     | 23:00:15
2022-04-03| 14:59:59 | 50     | 00:30:15
2022-04-03| 15:59:59 | 555    | 00:30:15
2022-04-03| 16:59:59 | 56     | 00:30:15

I want to get maximum value along with Date,ReceivedTime.

Expected Result:

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

Date       | Value  | ReceivedTime
2022-04-01 | 15     | 13:30:00
2022-04-02 | 25     | 23:00:15
2022-04-03 | 555    | 00:30:15

>Solution :

This answer assumes that, in the event of two or more records being tied on a given day for the same highest value, you want to retain the single record with the most recent ReceivedTime. We can use DISTINCT ON here:

SELECT DISTINCT ON (Date) Date, Value, ReceivedTime
FROM yourTable
ORDER BY Date, Value DESC, ReceivedTime DESC;
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