简体   繁体   中英

Get distinct row on the max column value

I need a row of the table on the max column value

SELECT  MAX(PromoFileVersion) AS PromoFileVersion
        , FYearWeek
        , WeekPriority
        , PromoEventId  
FROM PromoCalendar   
GROUP BY FYearWeek, WeekPriority, PromoEventId

But I am getting duplicate records

在此处输入图片说明

Any help?

SELECT PromoFileVersion, FYearWeek, WeekPriority, PromoEventId  
FROM PromoCalendar p
where PromoFileVersion = (select max(PromoFileVersion) PromoFileVersion
                          from PromoCalendar p1
                          where p1.FYearWeek = p.FYearWeek 
                          and p1.WeekPriority = p.WeekPriority
                          GROUP BY  FYearWeek, WeekPriority);

Here is a DEMO

You can use row_number() :

select pc.*
from (select pc.*, 
             row_number() over (partition by FYearWeek, WeekPriority order by PromoFileVersion desc) as seq
      from PromoCalendar  pc
     ) pc
where pc.seq = 1;

If you have ties with PromoFileVersion then you cas use dense_rank() instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM