如何按Test number列去重,取最大Start time对应行的全部列数据
解决方案
方案1:使用窗口函数(推荐,性能更优,代码简洁)
你用的是SQL Server语法,可直接用ROW_NUMBER()窗口函数实现需求,按[Test number]分组后按[Start time]倒序排列,取每组第一条即可返回全部列:
SELECT * FROM ( SELECT *, ROW_NUMBER() OVER (PARTITION BY [Test number] ORDER BY [Start time] DESC) AS rn FROM [Testing_ADB_Overview].[dbo].[HTT_DB] ) t WHERE rn = 1 ORDER BY [Start time]
说明:因为你明确[Start time]值唯一,不会出现同一[Test number]下多个相同最大时间的冲突,该方案完全适用
方案2:基于你现有语句改造(更易理解)
把你已经写好的最大时间查询作为子查询,和原表关联匹配两个核心字段即可拿到整行数据:
SELECT a.* FROM [Testing_ADB_Overview].[dbo].[HTT_DB] a INNER JOIN ( SELECT [Test number], MAX([Start time]) AS max_start_time FROM [Testing_ADB_Overview].[dbo].[HTT_DB] GROUP BY [Test number] ) b ON a.[Test number] = b.[Test number] AND a.[Start time] = b.max_start_time ORDER BY a.[Start time]
内容的提问来源于stack exchange,提问作者M.F.
相关产品推荐
相关产品推荐

