首页 > 解决方案 > 计算某些行 MySQL 的平均值

问题描述

我有 4 张桌子,如下图所示。我想打印CustomerName谁在 2020 年第四季度支付的费用低于 2020 年第四季度的平均支付成本。

在此处输入图像描述

我试过使用这段代码,但它不起作用。有人可以帮我解决它!

select c.CustomerName, p.PaymentDate
from Payment p, Invoice i, BookingReq b, Customer c
where p.InvoiceID = i.InvoiceNum
and i.RequestID = b.RequestID
and b.CustomerID = c.CustomerID
and p.PaymentDate between '2020-10-01' and '2020-12-31'
and p.PaymentAmount < avg(if(p.PaymentDate between '2020-10-01' and '2020-12-31', 
                             p.PaymentAmount, 
                             NULL));
;

标签: mysqlsqlaverage

解决方案


推荐阅读