简体   繁体   English

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

[英]calculating the sum of the max for MySQL select statement

Reading through similar threads, but I cannot find a reason as to why this isn't working: 通过类似的线程进行阅读,但是我找不到导致其不起作用的原因:

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;

Shouldn't this calculate the sum of the maximum invoice_total? 这不应该计算最大的发票总额吗? Keeps giving me 'unknown column' error when I add the sum in front of the max. 当我将总数加到最大值前时,总是出现“未知列”错误。

You need to name the column . 您需要命名列。 . . I would do so explicitly: 我会明确地这样做:

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;

Try this- give max(invoice_total) an alias and then use that in the outer select: 试试这个-给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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM