计算MySQL select语句的最大值之和

时间:2017-10-13 19:46:07

标签: mysql sql sum max

通过类似的线索阅读,但我找不到为什么这不起作用的原因:

select sum(max(i.invoice_total)) as 'Sum of Largest Unpaid Invoice'
from (select vendor_id, MAX(invoice_total)
from vendors v join invoices i using (vendor_id)
where i.payment_date is null
group by v.vendor_id) as alias;

不应该计算最大invoice_total的总和吗?继续给我“未知专栏”#39;在最大值前添加总和时出错

2 个答案:

答案 0 :(得分:2)

您需要为该列命名。 。 。我会明确地这样做:

select sum(max_invoice_total) as `Sum of Largest Unpaid Invoice`
from (select vendor_id, MAX(invoice_total) as max_invoice_total
      from vendors v join
           invoices i
           using (vendor_id)
      where i.payment_date is null
      group by v.vendor_id
     ) alias;

答案 1 :(得分:0)

试试这个 - 给max(invoice_total)一个别名,然后在外部选择中使用它:

select sum(max_invoice) as 'Sum of Largest Unpaid Invoice'
from (select vendor_id, MAX(invoice_total) as max_invoice
from vendors v join invoices i using (vendor_id)
where i.payment_date is null
group by v.vendor_id) as alias;