我有以下查询 -
select System_Id, TeamProjectSK, System_State, System_Rev
from
(
select w1.System_Id, w1.TeamProjectSK, w1.System_State, w1.System_Rev,
row_number() over(partition by w1.System_Id, w1.TeamProjectSK, w1.System_State order by w1.System_Rev desc) rn
from dbo.DimWorkItem w1
where w1.System_Id = 1691
) d
where rn = 1
order by System_Id, TeamProjectSK,System_Rev desc;
输出看起来像 -
System_Id TeamProjectSK System_State System_Rev
1691 126 Closed 17
1691 126 Resolved 14
1691 126 Active 13
1691 126 Proposed 2
但我希望我的输出选择具有最高“System_Rev”值的行,它应该看起来像 -
System_Id TeamProjectSK System_State System_Rev
1691 126 Closed 17
现在,我尝试使用以下查询重新使用“最大”概念。但这对我的帮助不起作用。请让我知道您的意见,或者是否有更好的方法来获得相同的结果。
select System_Id, TeamProjectSK, System_State, max(System_Rev)
from
(select System_Id, TeamProjectSK, System_State, System_Rev
from
(
select w1.System_Id, w1.TeamProjectSK, w1.System_State, w1.System_Rev,
row_number() over(partition by w1.System_Id, w1.TeamProjectSK, w1.System_State order by w1.System_Rev desc) rn
from dbo.DimWorkItem w1
where w1.System_Id = 1691
) d
where rn = 1
) e
group by System_Id, TeamProjectSK, System_State
having max(System_Rev)>1