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

Filtering records by the larger date value in MySql

I have the following table:

Deals

PositionID - INT
Deal - INT
Time - Datetime

For every position ID, there are 2 rows, 1 for opening the order and 1 for closing the order. The goal here is to get the larger date ( i.e. the closing date ).

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

When I execute the following script I get these fields.

select md.PositionID,md.Deal , md.Time from deals md 
where md.PositionID  in (5804179, 2027379)

enter image description here

The script should return:

5804179 -> 2022-06-28 08:12:58
2027379 -> 2021-02-14 23:21:21

I want to get the larger Time value for every PositionID. And I’ve tried to group by PositionID but not the results I wanted. So is there a way to do that ?

>Solution :

SELECT DISTINCT
       PositionID,
       FIRST_VALUE(Deal) OVER (PARTITION BY PositionID ORDER BY `Time` DESC) Deal,
       MAX(`Time`) OVER (PARTITION BY PositionID) `Time`
FROM deals
-- WHERE ...;
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