我使用此查询来获取:来自tblcustomers的customername来自针对每个客户的tbltransactions的transactionamount的总和
select a.customerid, sum(transactionamount) as transactionamount ,b.customername
from tbltransactions a
join tblcustomers b using (customerid)
group by a.customerid
order by b.customername
列transactionamount
中tbltransactions
包含客户购买的正值和客户付款的负值。
如何在此查询中为每个客户获得此表达式min(transactionamount)<0?
编辑:min(transactionamount)<0使我到目前为止每个客户支付的最大金额
您可以使用条件聚合来做到这一点:
select a.customerid, sum(transactionamount) as transactionamount, b.customername,
min(case when transactionamount < 0 then transactionamount end) as BiggestPayment
from tbltransactions a join
tblcustomers b
using (customerid)
group by a.customerid
order by b.customername;
请注意,小于0的最小值实际上是最小值而不是最大值。
编辑:
在这种情况下,我将使用substring_index()
/group_concat()
技巧找到最新的付款日期:
substring_index(group_concat((case when transactionamount < 0 then transactionamount end)
order by transactiondate desc
), ',', 1)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句