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

SQL get corresponding data where min or max value

Here is my data structure

ID_group Date Price
1 20/11/2022 3
1 19/11/2022 4
2 18/11/2022 42
2 19/11/2022 2
1 21/11/2022 2

I want to make a table in order to get my data in this format :

ID_group MaxPrice MaxPriceDate MinPrice MinPriceDate
1 4 19/11/2022 3 20/11/2022
2 42 18/11/2022 2 19/11/2022

Here is what I have now :

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

select ID_group,
max(price) MaxPrice,
'' MaxPriceDate,
min(price) MinPrice,
'' MinPriceDate
from table
group by ID_group

>Solution :

We can use ROW_NUMBER here, along with pivoting logic:

WITH cte AS (
    SELECT t.*, ROW_NUMBER() OVER (PARTITION BY ID_group ORDER BY Price) rn1,
                ROW_NUMBER() OVER (PARTITION BY ID_group ORDER BY Price DESC) rn2
    FROM yourTable t
)

SELECT
    ID_group,
    MAX(CASE WHEN rn2 = 1 THEN Price END) AS MaxPrice,
    MAX(CASE WHEN rn2 = 1 THEN "Date" END) AS MaxPriceDate,
    MAX(CASE WHEN rn1 = 1 THEN Price END) AS MinPrice,
    MAX(CASE WHEN rn1 = 1 THEN "Date" END) AS MinPriceDate
FROM cte
GROUP BY ID_group
ORDER BY ID_group;
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