第一张图片显示Paid_Amount
第2行和第3行的结果,重复付费金额,同时付费1 image 1
第二张图片显示Paid_Amount
仅存在一次image 2
我正在使用以下查询,付费表中只存在一条记录虽然重复3次,请帮我找出正确的结果。
SELECT
SUM(totalAmount) as TotalSum,
T_VISIT.dateVisit as Visit,
T_AMOUNT.amountType as Amount_Type,
(CASE
WHEN T_PAID_AMT_PT.paidAmt IS NOT NULL
THEN T_PAID_AMT_PT.paidAmt
ELSE 0 END) as Paid_Amount,
(T_PAID_AMT_PT.paidAmt - SUM(totalAmount)) as Balance
FROM tblPatientAmountDetails T_AMOUNT
INNER JOIN tblVisitDetailsPt T_VISIT
ON T_AMOUNT.visitId = T_VISIT.visitId
INNER JOIN tblPaidAmountPt T_PAID_AMT_PT
ON T_PAID_AMT_PT.visitId = T_VISIT.visitId
WHERE T_VISIT.regNo = 'OPD0002'
GROUP BY
T_VISIT.dateVisit,
T_AMOUNT.amountType,
T_PAID_AMT_PT.paidAmt,
T_PAID_AMT_PT.balence