0

编辑

这是我的数据示例:

 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子句中使用该函数吗?

4

3 回答 3

1

这是使用 cte 时的样子。

 DECLARE @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),
('Market2','Operator2','2012-12-01',3,5,7),
('Market3','Operator3','2012-12-01',1,2,7),
('Market4','Operator4','2012-12-01',2,1,7),
('Market5','Operator5','2012-12-01',0,4,7),
('Market6','Operator6','2012-12-01',2,44,7)

--with cte
    ;WITH Cte_Rank AS
    (
        SELECT 
             a.[Market],
             RANK() OVER (ORDER BY SUM(a.[Num Registrations] + a.[Num Active])) [Rnk]
        FROM @Table a GROUP BY a.Market 
    )
    SELECT 
        a.[Market],
        a.[Operator],
        a.[Date],
        a.[Num Registrations],
        a.[Num Active],
        a.[Num Accepted Purchases],
        c.Rnk
    FROM @Table a
    INNER JOIN Cte_Rank c ON c.Market = a.Market


--The only way to avoid subquery is to get rid of the "SUM" aggregate       
    SELECT 
        a.[Market],
        a.[Operator],
        a.[Date],
        a.[Num Registrations],
        a.[Num Active],
        a.[Num Accepted Purchases],
         RANK() OVER (ORDER BY a.[Num Registrations] + a.[Num Active]) [Rnk]
    FROM @Table a
于 2012-12-05T12:42:46.543 回答
1

是的,您根本不需要进行显式连接:

SELECT distinct
    a.[Market],
    a.[Operator],
    a.[Date],
    a.[Num Registrations],
    a.[Num Active],
    a.[Num Accepted Purchases],
    [rnk] = dense_rank() over (order by tot, market)
FROM (select a.*,
             sum(a.[Num Registrations] + a.[Num Active]) over (partition by market) as tot
      from xxx.dbo.tb_r12044dxx_yyyy a
     ) a

如果您没有重复项,则可以distinctselect.

于 2012-12-05T14:57:56.217 回答
1

我认为最后的 group by 是多余的,但我看不到避免子查询的方法..

我不确定以下是否更有效,您必须使用您的数据尝试它,但它更容易阅读有争议:

SELECT 
    a.[Market],
    a.[Operator],
    a.[Date],
    a.[Num Registrations],
    a.[Num Active],
    a.[Num Accepted Purchases],
    [rnk] = DENSE_RANK() OVER (ORDER BY NumToRankOver)
FROM xxx.dbo.tb_r12044dxx_yyyy a
INNER 
JOIN 
(
    SELECT 
    [Market],
    SUM([Num Registrations] + [Num Active]) AS NumToRankOver
    FROM xxx.dbo.tb_r12044dxx_yyyy
    GROUP BY [Market]
) b
    ON  a.[Market] = b.[Market]
于 2012-12-05T12:24:18.910 回答