我正在尝试计算到期金额总和,并在表i_payment_history中对更多金额进行分组,然后将结果与总数" to_be_payed"进行比较。表pt_bookings中保存的金额。所有涉及的字段都是十进制(8,2),我已经尝试使用Cast becouse而不是没有工作。价值"到期"只是“付款”的结果 - “付款”' 列中的第一个值"到期"应该是755,10-200,00 = 535,10。 为什么"到期"计算错误的值? Txs!
SELECT i_payments_history.booking_id, Cast(SUM(amount)AS DECIMAL(8,2)) as payed, pt_bookings.booking_total, pt_bookings.booking_ref_no, (pt_bookings.booking_total - payed) as due
FROM `i_payments_history`
JOIN pt_bookings
on pt_bookings.booking_id= i_payments_history.booking_id
GROUP BY booking_id
在下面的图片中查看查询返回的内容
答案 0 :(得分:2)
重复表达式:
SELECT ph.booking_id, Cast(SUM(ph.amount) AS DECIMAL(8,2)) as payed, b.booking_total, b.booking_ref_no,
(b.booking_total - SUM(ph.amount)) as due
FROM `i_payments_history` ph JOIN
pt_bookings b
on b.booking_id = ph.booking_id
GROUP BY b.booking_id;
别名不能在定义它的同一select
中使用。一种替代方法是使用子查询;另一种是重复表达。您的查询未返回错误,因为您的某个表必须包含名为payed
的列。