编辑
这是我的数据示例:
create TABLE #Table
(
[Market] VARCHAR(100),
[Operator] VARCHAR(100),
[Date] DATE,
[Num Registrations] INT,
[Num Active] INT,
[Num Accepted Purchases] INT
)
INSERT INTO #Table VALUES
('Market1','Operator1','2012-12-01',2,4,7),
('Market1','Operator2','2012-12-01',3,5,7),
('Market1','Operator3','2012-12-01',1,2,7),
('Market2','Operator4','2012-12-01',2,1,7),
('Market2','Operator5','2012-12-01',0,4,7),
('Market3','Operator6','2012-12-01',2,44,7)
我有以下脚本:
SELECT
a.[Market],
a.[Operator],
a.[Date],
a.[Num Registrations],
a.[Num Active],
a.[Num Accepted Purchases],
[rnk] = b.rnk
FROM
#Table a
INNER JOIN
(
SELECT
[Market],
[rnk] = RANK() OVER (ORDER BY SUM([Num Registrations] + [Num Active]))
FROM #Table
GROUP BY [Market]
) b
ON
a.[Market] = b.[Market]
GROUP BY
a.[Market],
a.[Operator],
a.[Date],
a.[Num Registrations],
a.[Num Active],
a.[Num Accepted Purchases],
b.rnk
以上工作正常,但我是否过于复杂了sub-query
; 我可以RANK
直接在主查询的SELECT
子句中使用该函数吗?