Multiplique a linha recém-inserida com outro valor da coluna e encontre Soma Total no SQL
Eu tenho 4 tabelas aqui, preciso multiplicar o valor da linha recém-inserida em uma tabela por outra linha e encontrar a soma total usandoCustomerId
:
Tabela de clientes:
CustomerId Name EmailId
-------------------------
1 Paul [email protected]
2 John [email protected]
LoyaltyPointTable:
LoyaltyPointsId LoyaltyType Points
---------------------------------------
1 Registration 10
2 Loginstatus 1
3 Downloading 10
4 Redemming 1
5 Sharing 20
6 Refer 10
LoyaltyDetailsTable:
LoyaltyDetailsId LoyaltyPointsId CustomerId Dates
-------------------------------------------------
1 1 1 2015-01-22
2 2 1 2015-01-22
3 3 2 2015-01-22
4 3 1 2015-01-22
5 4 1 2015-01-22
6 4 1 2015-01-24
7 5 1 2015-01-24
Esta consulta funciona bem para a soma total de cadaLoyaltyType
SELECT
LoayaltyPointsTable.LoyaltyType,
COUNT(CustomerTable.CustomerId) AS UserActions,
SUM(LoayaltyPointsTable.Points) AS TotalPoints
FROM
LoayaltyPointsTable
JOIN
LoyaltyDetailsTable ON LoayaltyPointsTable.LoyaltyPointsId = LoyaltyDetailsTable.LoyaltyPointsId
JOIN
CustomerTable ON CustomerTable.CustomerId = LoyaltyDetailsTable.CustomerId
WHERE
CustomerTable.CustomerId = 1
GROUP BY
LoyaltyDetailsTable.CustomerId ,LoayaltyPointsTable.LoyaltyType
abaixoRedeemPointsTable
é criado com relação à linha resgatada emLoyaltyPointTable
:
RedeemPointsTable:
RedeemPointsId CustomerId ShopName BillNo Amount
------------------------------------------------
1 1 Mall x 4757 100
3 1 Mall y SH43 50
4 1 Mall x 7743 10
6 1 Mall x s34a 60
O que estou esperando é antes de calcular a soma total, quero a colunaAmount
soma(100+50+10+60) * 1
noRedeeming
noLoyaltyPointTable
a ser adicionado com o total de pontos para cadaCustomerId
Saída esperada
LoyaltyType UserActions TotalPoints
-------------------------------------
Downloading 1 10
Loginstatus 1 1
Redemming 4 (100+50+10+60)*1(here using Amount in RedeemPointsTable)
Refer 1 10
Registration 1 10
Sharing 1 20
A contagem de ações do usuário é 4, é baseada noAmount
ele entrouRedeemPointsTable
Devo precisar fazer alterações ao adicionar uma coluna de chave estrangeira noRedeemPointsTable
ou você pode apontar meu erro?
Qualquer ajuda seria ótimo.