4

Please see my code below as it is running too slowly with the CROSS APPLY.

How can I remove the CROSS APPLY and add something else that will run faster? Please note I am using SQL Server 2008 R2.

;WITH MyCTE AS
(
   SELECT 
      R.NetWinCURRENCYValue AS NetWin
     ,dD.[Date]             AS TheDay
   FROM   
      dimPlayer AS P
   JOIN 
      dbo.factRevenue AS R ON P.playerKey = R.playerKey
   JOIN 
      dbo.vw_Date AS dD ON Dd.dateKey = R.dateKey
   WHERE    
      P.CustomerID   = 12345)
SELECT 
     A.TheDay               AS [Date]
    ,ISNULL(A.NetWin, 0)    AS NetWin
    ,rt.runningTotal        AS CumulativeNetWin
FROM MyCTE AS A
CROSS APPLY (SELECT SUM(NetWin) AS runningTotal 
                  FROM MyCTE WHERE TheDay <= A.TheDay) AS rt
ORDER BY A.TheDay
4

3 回答 3

0

正如我在这里所说,如果您想要真正快速的计算,请将其放入具有顺序主键的临时表中,然后计算滚动总数:

create table #Temp (
    ID bigint identity(1, 1) primary key,
    [Date] date,
    NetWin decimal(29, 10)
)

insert into #Temp ([Date], NetWin)
select
    dD.[Date],
    sum(R.NetWinCURRENCYValue) as NetWin,
from dbo.dimPlayer as P
    inner join dbo.factRevenue as R on P.playerKey = R.playerKey
    inner join dbo.vw_Date as dD on Dd.dateKey = R.dateKey
where P.CustomerID = 12345
group by dD.[Date]
order by dD.[Date]

;with cte as (
    select T.ID, T.[Date], T.NetWin, T.NetWin as CumulativeNetWin
    from #Temp as T
    where T.ID = 1

    union all

    select T.ID, T.[Date], T.NetWin, T.NetWin + C.CumulativeNetWin as CumulativeNetWin
from cte as C
    inner join #Temp as T on T.ID = C.ID + 1
)
select C.[Date], C.NetWin, C.CumulativeNetWin
from cte as C
order by C.[Date]

我假设您可以在输入中有重复的日期,但不希望在输出中有重复的日期,因此我在将数据放入表之前对其进行了分组。

于 2013-08-29T16:22:47.463 回答
0

这里https://stackoverflow.com/a/13744550/613130建议使用递归 CTE。

;WITH MyCTE AS
(
    SELECT 

           R.NetWinCURRENCYValue AS NetWin
          ,dD.[Date]             AS TheDay
          ,ROW_NUMBER() OVER (ORDER BY dD.[Date]) AS RN

    FROM   dimPlayer             AS P

        JOIN dbo.factRevenue     AS R   ON P.playerKey = R.playerKey
        JOIN dbo.vw_Date         AS dD  ON Dd.dateKey = R.dateKey

    WHERE    P.CustomerID   = 12345

)

, MyCTERec AS
(
    SELECT  C.TheDay               AS [Date]
           ,ISNULL(C.NetWin, 0)    AS NetWin
           ,ISNULL(C.NetWin, 0)    AS CumulativeNetWin
           ,C.RN

    FROM MyCTE AS C
    WHERE C.RN = 1

    UNION ALL

    SELECT  C.TheDay               AS [Date]
           ,ISNULL(C.NetWin, 0)    AS NetWin
           ,P.CumulativeNetWin + ISNULL(C.NetWin, 0)    AS CumulativeNetWin
           ,C.RN

    FROM MyCTERec P
    INNER JOIN MyCTE AS C ON C.RN = P.RN + 1
)

SELECT * 
    FROM MyCTERec 
    ORDER BY RN
    OPTION (MAXRECURSION 0)

请注意,如果您有 1 条记录 == 1 天,则此查询将起作用!如果一天中有多条记录,则结果将与其他查询不同。

于 2013-08-29T13:36:49.330 回答
0
CREATE TABLE #temp (NetWin money, TheDay datetime)
insert into #temp 
SELECT 
      R.NetWinCURRENCYValue AS NetWin
     ,dD.[Date]             AS TheDay
   FROM   
      dimPlayer AS P
   JOIN 
      dbo.factRevenue AS R ON P.playerKey = R.playerKey
   JOIN 
      dbo.vw_Date AS dD ON Dd.dateKey = R.dateKey
   WHERE    
      P.CustomerID   = 12345;

SELECT 
     A.TheDay               AS [Date]
    ,ISNULL(A.NetWin, 0)    AS NetWin
    ,SUM(B.NetWin)          AS CumulativeNetWin
FROM #temp AS A 
JOIN #temp AS B 
  ON A.TheDay >= B.TheDay
GROUP BY A.TheDay, ISNULL(A.NetWin, 0);
于 2013-08-29T13:45:46.843 回答