我正在尝试创建一个将选择DISTINCT行的查询,使用修订版Minor / Major方案进行选择。下面是一个示例表:
Serial Number | RevMajor | RevMinor
-----------------------------------
AQ155 | 1 | 1
AQ155 | 1 | 2
AQ155 | 1 | 1
AQ155 | 1 | 7
AQ155 | 2 | 1 <---------
JR2709 | 1 | 7
JR2709 | 2 | 2 <---------
如何在T-SQL 2008中编写一个只选择两条突出显示的行的查询,即“最新版本”? 提前谢谢!
答案 0 :(得分:1)
你可以
select * from (
select *, row_number() over (partition by [Serial Number] order by RevMajor desc, RevMinor desc) VersionRank
from table
) T
where VersionRank = 1
答案 1 :(得分:0)
select [serial number], revmajor, revminor
from table1
where revMajor = (select max(revmajor) from table1)
另一种方法可以是:
select [serial number], revmajor, revminor
from table1 a
inner join ( select max(revMajor) from table1 ) b on a.revmajor = b.revmajor
另一种方式,如果你知道只有2行:
select top 2 [serial number], revmajor, revminor
from table1 a
order by revmajor desc, revminor desc