返回平均存款,在sql server中退出最后八周

时间:2015-12-28 09:11:35

标签: sql sql-server

我想写一个简单的查询,它会返回MyTable的平均存款和押注,但从今天开始的最后八周,以及格式为dd / mm的那一周的第一天选择

     SELECT 
             [PlayerId],
             SUM(Aggr.DepositAmount)                                                    [Deposits],
             SUM(Aggr.DepositCount)                                                     [DepositCount],
             SUM(Aggr.WithdrawalAmount)                                                 [Withdrawals],                      
             SUM(Aggr.WithdrawalCount)                                                  [WithdrawalCount]
      FROM [WarehouseMgmt].[FactPaymentAgr] Aggr
      WHERE (TimeId BETWEEN @CurrentWeek AND @CurrentWeek7)
      GROUP BY [PlayerId]

之后我将计算平均存款和取款

所以最后的结果我想成为像

这样的东西

周,Week_in_format_dd /毫米,PlayerId,average_depos,average_withdraw

1 个答案:

答案 0 :(得分:1)

我已更新我的查询以符合您的上一个要求

DECLARE @first_date DATETIME

SET @first_date = DATEADD(ww, -8, GETDATE())

SELECT T.week_number,
            T.date_formatted,
            T.playerID,
            AVG(DepositAmount) AS avg_deposit,
            AVG(WithdrawalAmount) AS avg_withdrawal
    FROM
        (SELECT 
                DATEPART(ww, timeID) AS week_number,
                SUBSTRING(CONVERT(VARCHAR, timeID, 103), 1, 5) AS date_formatted,
                PlayerID,
                DepositAmount,
                WithdrawalAmount)
        FROM [WarehouseMgmt].[FactPaymentAgr] Aggr
        WHERE TimeId >= @first_date) AS T
    GROUP BY week_number,
             date_formatted,
             playerID