在过去的2天里,我对此查询提出了挑战。
我到处搜寻,但徒劳无功。
我有3张桌子; customers
,transactions
和withdrawals
。
客户表具有帐户号,名称等,交易具有FK帐户号和金额,而提款具有FK帐户号和金额。
我只想选择AccountNumber,客户姓名和SUM
相应的交易金额,然后按AccountNumber对取款金额AccountNumber求和。
目前我被迫跑步
SELECT AccountNumber,FirstName,LastName,Town,Contact
FROM customers
WHERE status = 1
ORDER BY AccountNumber ASC
将数据加载到数据表中并循环遍历以选择:
SELECT SUM(SusuAmount)
FROM transactions
WHERE AccountNumber = " + account + "
AND Approval = 1`
还有这个:
SELECT SUM(ValueAmount)
FROM withdrawals
WHERE AccountNumber = " + account + "
AND Approval = 1`
请提出一个查询以帮助您完成一次。
最佳答案
试试这个查询:
SELECT cus.AccountNumber,cus.FirstName,cus.LastName,cus.Town,cus.Contract,Sum(trans.SusuAmount) AS TransactionAmount,SUM(widraw.ValueAmount) AS WithdrawalsAmount FROM customers AS cus
LEFT JOIN transactions AS tran ON (trans.AccountNumber=cus.AccountNumber AND cus.Approval=1)
LEFT JOIN withdrawals AS widraw ON (widraw.AccountNumber=cus.AccountNumber AND widraw.Approval=1)
WHERE cus.status=1 ORDER BY cus.AccountNumber ASC
关于c# - 从表1总和表2,列总和表3,基于表accountNumbers的列中选择,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40355317/