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

はい、明示的な結合を行う必要はまったくありません。

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

重複がない場合は、distinctから削除できますselect

于 2012-12-05T14:57:56.217 に答える
1

最後のgroupbyは冗長だと思いますが、サブクエリを回避する方法がわかりません。

以下がもっと効率的かどうかはわかりません。データで試してみる必要がありますが、間違いなく読みやすくなっています。

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 に答える
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 に答える