加入选择错误的总数 [英] Join selecting wrong totals

查看:70
本文介绍了加入选择错误的总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好



我的查询如下:



Hi

I have a query which is as follows:

SELECT     Prepaid.RedeemerID, OneTimePin.WithdrawerID, SUM(Prepaid.Amount) AS PrepaidAmountSum, SUM(OneTimePin.Amount) AS OneTimePinSum, SUM(Prepaid.Amount)- SUM(OneTimePin.Amount) AS 'Difference'
FROM         Prepaid, OnetimePin
WHERE   (Prepaid.RedeemerID = OneTimePin.WithdrawerID) AND (Prepaid.IsActive = 1) AND (OneTimePin.IsActive = 1) AND (Prepaid.IsRedeemed = 1) AND (OneTimePin.IsPaid = 1)
GROUP BY  Prepaid.RedeemerID,OneTimePin.WithdrawerID
ORDER BY 'Difference', Prepaid.RedeemerID, OneTimePin.WithdrawerID





但结果不正确,它为onetimepin总和选择和疯狂金额,也几乎是预付金额的两倍。



请能任何人都会帮我解释为什么会发生这种情况。



redeemerID和withdrawerID是一回事。



我似乎无法弄清楚为什么会这样......



请帮助



but the results are incorrect, it selects and insane amount for the onetimepin sum and also almost double for the prepaid amount sum.

Please can anyone help me with why this is happening.

The redeemerID and withdrawerID are the same thing.

I can't seem to figure out why this is happening...

Please help

推荐答案

你没有加入任何东西 - 这可能是你的问题。

我不确定你想要达到的目标,但你是亲想要开始:

You aren't JOINing anything - which is probably your problem.
I am not exactly sure what you are trying to achieve, but you probably want to start with:
SELECT p.RedeemerId, SUM(p.Amount), SUM(o.Amount), SUM(p.Amount) - SUM(o.Amount)
FROM Prepaid p
JOIN OteTimePin o
ON p.redeemerId = o.WithdrawerId
GROUP BY p.RedeemerId


这篇关于加入选择错误的总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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