mysql使用sum和max来查找最高的支付发票

时间:2018-11-07 23:43:43

标签: mysql sql sum subquery max

我正试图弄清楚如何从表中获得最大的SUM值结果,以便显示收据的最高支付金额。我也应该使用subselect,但是我感到困惑。 到目前为止,此查询为我提供了我需要显示的所有信息(最大值除外)。 This is the table (invoice+OD) need to be used This is what i am currently getting This is what i should be getting

我可以在哪里添加MAX()(总和)?

SELECT invoicenumber invoice
     , invoicedate date
     , SUM(orderqty*ordersell) AS 'Invoice Total' 
  FROM invoice 
  JOIN orderdetail 
    ON invoice.invoiceid = orderdetail.invoiceid 
 GROUP 
    BY invoice ;

3 个答案:

答案 0 :(得分:0)

如果在SELECT中使用SUM或MAX,则需要GROUP BY其他列:

SELECT invoicenumber AS invoice
 , invoicedate AS date
 , SUM(orderqty*ordersell) AS 'Invoice Total' 
FROM invoice 
JOIN orderdetail ON invoice.invoiceid = orderdetail.invoiceid 
GROUP BY invoicenumber,invoicedate;

答案 1 :(得分:0)

您可以尝试执行此操作(在上一个答案的基础上):

1.7.0.13.5

效率可能不高,但仅返回最高结果。

答案 2 :(得分:0)

我不知道这与您所寻找的内容是否相近,但我认为这会起作用(如果我正确理解了您要实现的目标)。

SELECT inv.invoicenumber,
       inv.invoice, 
       inv.invoicedate AS 'Date',
       MAX((SELECT ord.orderqty+ord.ordersell FROM DUAL)) AS 'Invoice Total' 
  FROM invoice inv
  JOIN orderdetail ord
    ON inv.invoice.invoiceid = ord.orderdetail.invoiceid 
 GROUP BY inv.invoice, 
          inv.invoice, 
          inv.invoicedate;