我有一张桌子#results,我在表格中填写了一些值,但是如果我没有匹配,那么在过去的8周内,它不会填满表格
这是我的插入和合并代码
INSERT INTO #Results (
[PlayerId],
[CalendarWeekID],
[TotalDepositAmount],
[TotalDepositCount],
[TotalWithdrawalAmount],
[TotalWithdrawalCount]
)
SELECT
[PlayerId],
[CalendarWeekID],
SUM(Aggr.DepositAmount) [Deposits],
SUM(Aggr.DepositCount) [DepositCount],
SUM(Aggr.WithdrawalAmount) [Withdrawals],
SUM(Aggr.WithdrawalCount) [WithdrawalCount]
FROM [WarehouseMgmt].[FactPaymentAgr] Aggr
JOIN WarehouseMgmt.DimTime DT on DT.Id = Aggr.TimeId
WHERE (CalendarWeekId BETWEEN @CurrentWeek7 AND @CurrentWeek) AND [PlayerId] = @PlayerID
GROUP BY [PlayerId],[CalendarWeekID]
MERGE #Results AS Target
USING( SELECT
[PlayerId],
[CalendarWeekID],
SUM(Aggr.FinancialBetAmount) [TotalBetAmount],
SUM(Aggr.FinancialBetCount) [TotalBetCount]
FROM [WarehouseMgmt].[FactWalletAgr] Aggr
JOIN [WarehouseMgmt].[DimPlayer] DPL ON Aggr.[PlayerId] = DPL.[Id]
JOIN WarehouseMgmt.DimTime DT on DT.Id = Aggr.TimeId
WHERE (CalendarWeekId BETWEEN @CurrentWeek7 AND @CurrentWeek) AND [PlayerId] = @PlayerID
GROUP BY [PlayerId],[CalendarWeekID]) AS Source
ON (Target.[PlayerId] = Source.[PlayerId])
WHEN MATCHED THEN
UPDATE SET Target.[TotalBetAmount] = Source.[TotalBetAmount],
Target.[TotalBetCount] = Source.[TotalBetCount] ;
一名玩家的当前结果将采用此格式
PlayerId一些数量和数量CalendarWeekId CalendarWeek 1110,10,1,... 201553(周id) 1110,1000,23,... 201547(weekId)
但我想要的是表格中所有过去8周的数据,即使它是0 所以正确的格式将是
缺少的行应为0
答案 0 :(得分:0)
您只包含一位玩家的数据,因此您可以使用td
解决此问题。 left join
语句应如下所示:
insert