我正在处理的问题是关于发生在where子句之外的付款。
如果我在2020年10月1日和2020年10月20日分别支付了两笔款项,每个付款20美元,我需要看到支付总额为40美元。但是,金额会根据日期筛选器进行更改。
如果我选择10/1-10/20,它将显示支付总额为40,但如果我选择10/15-10/20,它将仅显示20。我有3列,分别名为Bill、total Paid、amount it。支付的总额应该保持停滞。
示例查询
Select b.name as bill, bp.amount as 'amount paid'
from bill b
left join billpayment bp on b.id=bp.bill_id
where bp.date between Startdate and Enddate所以这给了我付款,但我希望它添加任何发生在日期范围之外的付款。
发布于 2020-11-18 05:43:37
如果想要总体合计,请在子查询中使用窗口函数:
select bp.*
from (Select b.name as bill, bp.amount as amount_paid,
sum(bp.amount) over () as total_amount
from bill b left join
billpayment bp
on b.id = bp.bill_id
) bp
where bp.date between Startdate and Enddatehttps://stackoverflow.com/questions/64883731
复制相似问题