将新输入的行与另一列值相乘,并在 SQL 中找到 Total Sum [英] Multiply newly entered row with another column value and find Total Sum in SQL

查看:21
本文介绍了将新输入的行与另一列值相乘,并在 SQL 中找到 Total Sum的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有 4 个表,我需要将表中新输入的行值与另一行相乘,然后使用 CustomerId 找到总和:

I have 4 tables here, I need to multiply newly entered row value in a table with another row and find the total sum using CustomerId:

客户表:

CustomerId Name   EmailId
-------------------------
1          Paul   r@r.com
2          John   J@j.com

忠诚度积分表:

LoyaltyPointsId LoyaltyType     Points
---------------------------------------
1               Registration       10
2               Loginstatus         1
3               Downloading        10
4               Redemming           1
5               Sharing            20
6               Refer              10

忠诚度详情表:

 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 

此查询适用于每个 LoyaltyType 的总和

This query works fine for the total sum for each LoyaltyType

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 

下面的RedeemPointsTable 是根据LoyaltyPointTable 中的兑换行创建的:

below RedeemPointsTable is created with relation to row redeeming in LoyaltyPointTable:

兑换积分表:

 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

我期望的是在计算总和之前,我想要列 Amount sum (100+50+10+60) * 1 in RedeemingLoyaltyPointTable 中添加每个 CustomerId

What I am expecting is before calculating the total sum, I want column Amount sum (100+50+10+60) * 1 in Redeeming in LoyaltyPointTable to be added with total points for each CustomerId

预期输出

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

用户操作数为4,是根据他在RedeemPointsTable

User actions count is 4, it is based on the Amount he entered in RedeemPointsTable

RedeemPointsTable 中添加外键列是否需要进行更改,或者您能指出我的错误吗?

Should I need to make changes in adding a foreign key column in RedeemPointsTable or can you point out my mistake?

任何帮助都会很棒.

推荐答案

这是返回期望结果的查询:

This is the query which returns desired result:

SELECT 
   LoyaltyPointTable.LoyaltyType, 
   CASE 
      WHEN LoyaltyPointTable.LoyaltyPointsId=4 THEN (SELECT COUNT(amount) FROM RedeemPointsTable where CustomerId=1) 
      ELSE COUNT(CustomerTable.CustomerId) 
   END as UserActions, 
   CASE 
      WHEN LoyaltyPointTable.LoyaltyPointsId=4 THEN (SELECT SUM(amount) FROM RedeemPointsTable where CustomerId=1)*Points
      ELSE SUM(LoyaltyPointTable.Points) 
    END as TotalPoints
FROM 
   LoyaltyPointTable 
JOIN 
   LoyaltyDetailsTable ON LoyaltyPointTable.LoyaltyPointsId =     LoyaltyDetailsTable.LoyaltyPointsId
JOIN
   CustomerTable ON CustomerTable.CustomerId = LoyaltyDetailsTable.CustomerId 
WHERE
   CustomerTable.CustomerId = 1
GROUP BY
   LoyaltyDetailsTable.CustomerId ,LoyaltyPointTable.LoyaltyType 

你可以检查它这里

这篇关于将新输入的行与另一列值相乘,并在 SQL 中找到 Total Sum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆