我想使用前 5 名来捕获前 5 名的一些标准。
数据样式是月份日期、区域 1、子区域 1、子区域 2 等,类型,每行的唯一标识符。
我正在尝试做的是每个月和区域找到前 5 种类型。
样本数据:
Date Division Sub Division ID
31/05/2012 Sales 001
31/05/2012 Sales Call Centre 002
31/05/2012 Sales Call Centre 003
31/05/2012 Sales Store 004
31/05/2012 Marketing 005
31/05/2012 Marketing TV 006
30/04/2012 Sales 001
30/04/2012 Sales Call Centre 002
30/04/2012 Sales Call Centre 003
30/04/2012 Sales Store 004
30/04/2012 Marketing 005
30/04/2012 Marketing TV 006
等等。
我追求的输出是: 每个月在部门级别的前 5 名(不考虑子部门) 每个月在每个子部门级别的前 5
样本输出:
Date Top5Areas Volume
31/05/2012 Sales 100
31/05/2012 Marketing 90
31/05/2012 HR 50
30/04/2012 Sales 100
30/04/2012 Marketing 90
30/04/2012 HR 50
并根据各个区域的细分级别:
Date Top5Areas Volume
31/05/2012 Call Centre 100
31/05/2012 Store 90
31/05/2012 HR 50
但每组 5 个。
我不应该如何做到这一点,并想知道我是否需要通过一些查询来建立结果,或者是否有更好的方法。
在此先感谢并感谢您的建议。
SELECT [TableA].[DisplayVar] AS DisplayVar, [TableB].[Date] AS MonthDate, TableC. [Divison] AS Divison, Volume
FROM (SELECT [TableA].[DisplayVar],
[TableB].[Date],
TableC.[Divison],
Volume
( SELECT COUNT() + 1
FROM ( SELECT [TableA].[DisplayVar], [TableB].[Date], TableC.[Divison], COUNT() AS Volume
FROM ([TableA] INNER JOIN [TableB] ON [TableA].[ID] = [TableB].[ID]) LEFT JOIN TableC ON [TableB].ID = TableC.Descriptor
GROUP BY [TableA].[DisplayVar], [TableB].[Date], TableC.[Divison]
) AS T
WHERE T.[TableB].[Date] = Data.[TableB].[Date]
AND T.Volume > Data.Volume
) AS Rank
FROM ( SELECT [TableA].[DisplayVar], [TableB].[Date], TableC.[Divison], COUNT(*) AS Volume
FROM ([TableA] INNER JOIN [TableB] ON [TableA].[ID] = [TableB].[ID]) LEFT JOIN TableC ON [TableB].ID = TableC.Descriptor
WHERE [TableB].[Date] BETWEEN Date() AND DateADD("m", -12, Date())
GROUP BY[TableA].[DisplayVar], [TableB].[Date], TableC.[Divison]
) AS DATA
) AS Data
WHERE (((Data.Rank)<=5))
ORDER BY [TableB].[Date] DESC , Volume DESC;