1

how can i write my sql server query to update correct amount. Here is what i have at the moment, but it's not the correct solution.

I am trying to calculate sum of REdeemedBonusAmount by PlayerId and PlayerBonusId, but i need to use DBT.ExternalWAllet flag to take correct Amount from db

UPDATE R SET          
        [RedeemedBonusAmount] = AGGR.[RedeemedBonusAmount]
    FROM #Results R
    JOIN 
    (
        SELECT  Aggr.[PlayerId],
                aggr.[PlayerBonusId],
                    CASE 
                    WHEN DBT.ExternalWallet=1 THEN SUM(Aggr.Amount1)
                    ELSE SUM(Aggr.[Amount2]) 
                END AS [RedeemedBonusAmount]
        FROM table_1 Aggr
        JOIN table_2 DPB ON Aggr.[PlayerId] = DPB.[PlayerId]
        JOIN table_3 DB ON DPB.BonusId = DB.Id
        JOIN table_4 DBT ON DB.BonusTypeId= DBT.Id
        WHERE  Aggr.[TimeId] BETWEEN 2018110100 AND 2018122123 and aggr.playerid=362920
        GROUP BY Aggr.[PlayerId],aggr.[PlayerBonusId],DBT.ExternalWallet
    ) AS AGGR ON R.playerid=Aggr.playerid and R.PlayerBonusId=Aggr.[PlayerBonusId]

Here is the table what i get if i just run select:

PlayerId    PlayerBonusId   RedeemedBonusAmount
362920      619622          380.000000
362920      619624          19.000000
362920      619622          0.000000
362920      619624          0.000000
4
  • 3
    what are you trying to do? please provide Minimal, Complete, and Verifiable example Commented Dec 20, 2018 at 9:56
  • I am trying to calculate sum of REdeemedBonusAmount by PlayerId and PlayerBonusId, but i need to use DBT.ExternalWAllet flag to take correct Amount from db Commented Dec 20, 2018 at 9:59
  • 1
    Take the sum out of the case: SUM(CASE...) Commented Dec 20, 2018 at 10:02
  • For the sake of Good, add more context and some test data for trying to help you. Commented Dec 20, 2018 at 10:02

1 Answer 1

2

Instead of CASEing the SUM's, SUM the CASE.

Then you don't also need to GROUP on DBT.ExternalWallet, which isn't used in the join of the outer query.

UPDATE R 
SET [RedeemedBonusAmount] = AGGR.[RedeemedBonusAmount]
FROM #Results R
JOIN 
(
  SELECT  Aggr.[PlayerId],
          Aggr.[PlayerBonusId],
          SUM(CASE 
              WHEN DBT.ExternalWallet=1 THEN Aggr.Amount1
              ELSE Aggr.[Amount2]
              END) AS [RedeemedBonusAmount]
   FROM table_1 Aggr
   JOIN table_2 DPB ON Aggr.[PlayerId] = DPB.[PlayerId]
   JOIN table_3 DB ON DPB.BonusId = DB.Id
   JOIN table_4 DBT ON DB.BonusTypeId= DBT.Id
   WHERE  Aggr.[TimeId] BETWEEN 2018110100 AND 2018122123 and aggr.playerid=362920
   GROUP BY Aggr.[PlayerId],aggr.[PlayerBonusId]
) AS AGGR ON R.playerid=Aggr.playerid and R.PlayerBonusId=Aggr.[PlayerBonusId]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.