找出员工的总余额

时间:2015-04-14 12:14:00

标签: mysql

SELECT c.cName,(c.nOpBalance+CT.nAmount) DrAMount
FROM Customer c
INNER JOIN Client_Transaction CT ON CT.nClientPk = C.nCustomerPk 
AND CT.cTransRefType='PAYMENT'
GROUP BY c.cName

我怎样才能找到总金额。请提前帮助我。

以下是来自客户交易表数据的数据。

`nTransPk`, `dTransDate`, `nClientPk`, `cClientType`, `nTransRefPk`, `    cTransRefType`, `nAmount`, `cRemarks`
(1, '2015-04-06 00:00:00', 75, 'Samsu Bhai', 0, 'PAYMENT', 50, 'rere'),
(2, '2015-04-14 00:00:00', 30, 'PROVIDER', 0, 'PAYMENT', 12, 'fdf')

这就是客户表:

`nCustomerPk`, `cName`, `cAddress`, `cCity`, `cState`, `cPincode`, `cContactPerson`, `cWorkPhone`, `cHomePhone`, `cCellNo`, `cFaxNo`, `cEmail`, `cLstNo`, `cCstNo`, `cTinNo`, `dLstDate`, `dCstDate`, `nOpBalance`, `dOpBalDate`, `bIsActive`
(2, 'SAURABH', 'NA', 'Udaipur', 'Rajasthan', '313001', '', '', '', '434344545', '', '45Q2452Q45DFBSDGQ345Q3456', '', '', NULL, '2015-04-23 00:00:00', '2015-05-05 00:00:00', 99999999, '2015-04-23 00:00:00', 1),
(3, 'Nitin', 'D', 'Jaipur', 'Rajasthan', '444', 'c', '45', '545', '545', '45', 'tesrt563@4654565465.com', '', '', NULL, '1900-01-01 00:00:00', '1900-01-01 00:00:00', 1111, '1900-01-01 00:00:00', 1),

1 个答案:

答案 0 :(得分:0)

您需要使用SUM()来计算每个组的所有金额。使用LEFT JOIN代替INNER JOIN来吸引没有交易的客户。

Select c.cName,SUM(c.nOpBalance+IFNULL(CT.nAmount, 0)) DrAMount 
From Customer c
LEFT JOIN Client_Transaction CT ON CT.nClientPk = C.nCustomerPk 
    AND CT.cTransRefType='PAYMENT'
Group By c.cName