我试图通过@numRows参数限制交叉应用中所选内容的结果,这不起作用。我可以稍后使用' Row_Num'领域,但希望改善这一点。还有一些其他领域,但这是一般的想法。有什么想法/建议吗?
declare @numRows as int = 2
select
t1.Field_1
,t1.Field_2
,t1.Quantity
,[Row_Num] = ROW_NUMBER() over (partition by t1.Field_1, t1.Field_2 order by Quantity desc)
from
spTable as t1
cross apply
(
select top (@numRows)
Field_1, Field_2
from
spTable as t2
where
t1.Field_2 = t2.Field_2
and
t1.Field_1= t2.Field_1
) as t