简体   繁体   English

在最大列值上获取不同的行

[英]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() :您可以使用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.如果您与PromoFileVersion有联系,那么您 cas 可以使用dense_rank()代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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