答案已经在您的标题中,由SalesRep
和AccountID
和 Rank by划分Total Contact Value
。
解决方案将SQL Server
如下所示:
DECLARE @minimumValue decimal(20,2) = 10000
DECLARE @numberOfAccounts int = 35
DECLARE @from datetime = '1/1/2013'
DECLARE @till datetime = DATEADD(MONTH, 1, @from)
SELECT
[sub].[Sales Rep],
[sub].[Rank],
[sub].[Account ID],
[sub].[Total Contract Value]
FROM
(
SELECT
[Sales Rep],
[Account ID],
[Total Contract Value],
DENSE_RANK() OVER (PARTITION BY [Sales Rep] ORDER BY [Total Contract Value] DESC) AS [Rank]
FROM [Sales]
WHERE
[Total Contract Value] >= @minimumValue
AND [Date] > @from
AND [Date] < @till
) AS [sub]
WHERE [sub].[Rank] <= @numberOfAccounts
ORDER BY
[Sales Rep] ASC,
[Rank] ASC
这是一个(简单的)Sql Fiddle。