簡體   English   中英

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

[英]Get distinct row on the max column value

我需要在最大列值上的一行表格

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

但我收到重復的記錄

在此處輸入圖片說明

有什么幫助嗎?

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);

這是一個演示

您可以使用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;

如果您與PromoFileVersion有聯系,那么您 cas 可以使用dense_rank()代替。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM