1

我正在尝试创建一个将选择 DISTINCT 行的查询,使用修订次要/主要方案进行选择。下面是一个示例表:

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 中编写一个查询,只选择两个突出显示的行,即“最新修订版”?
提前致谢!

4

2 回答 2

1

你可以

select * from (
    select *, row_number() over (partition by [Serial Number] order by RevMajor desc, RevMinor desc) VersionRank
    from table
) T
where VersionRank = 1
于 2013-07-17T15:05:05.467 回答
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
于 2013-07-17T14:58:46.370 回答