我试图找到TotalPoints总和相减,如果新的条目在表中进行,
TotalPoints查询:
SELECT SUM(t.TotalUserActions) as Actions, sum(t.AllTotalPoints) as TotalPoints,
(select Name from CustomerTable where CustomerId=1) as Name from
(
SELECT CASE
WHEN LoayaltyPointsTable.LoyaltyPointsId=4 THEN (SELECT COUNT(amount)
FROM RedeemPointsTable where CustomerId=1)
ELSE COUNT(CustomerTable.CustomerId)
END as TotalUserActions , CustomerTable.Name,
CASE
WHEN LoayaltyPointsTable.LoyaltyPointsId=4 THEN (SELECT SUM(amount)
FROM RedeemPointsTable where CustomerId=1)*Points
ELSE SUM(LoayaltyPointsTable.Points)
END as AllTotalPoints
FROM
LoayaltyPointsTable
INNER JOIN
LoyaltyDetailsTable
on LoayaltyPointsTable.LoyaltyPointsId = LoyaltyDetailsTable.LoyaltyPointsId
INNER JOIN
CustomerTable
on CustomerTable.CustomerId = LoyaltyDetailsTable.CustomerId
where CustomerTable.CustomerID =1
group by CustomerTable.Name,LoayaltyPointsTable.LoyaltyPointsId,
LoayaltyPointsTable.Points,CustomerTable.CustomerId
) t
Totalpoints查询输出:
Actions Totalpoints Name
30 500 John
PriceTable
Priceid Title Discriptions Pricepoints
1 abc abc 400
2 def def 500
PriceClaimTable
PriceClaimId CustomerId PriceId
1 1 22
2 2 23
使用上面的表,我想减TotalPoints - 基于在客户和PriceId在PriceClaimTable Pricepoints如果有基于在客户中PriceClaimTable没有新的条目,那么就显示Totalpoints没有减法
迄今为止,我试图找到价位
select PriceTable.PricePoints from PriceTable
inner join PriceClaimTable
on PriceTable.PriceId = PriceClaimTable.PriceId
inner join CustomerTable
on CustomerTable.CustomerId = PriceClaimTable.CustomerId
where CustomerTable.CustomerId =1
group by PriceTable.PricePoints
这给了我输出:
PricePoints
400
预期输出:
TotalPoints:
100 // (500-400)
我怎样才能减去一个查询的结果,发现基于客户ID总得分总和?
其他的表结构:
http://sqlfiddle.com/#!2/67436/5
任何帮助将是巨大的。