查询不计算Invoice_Total列的总和

时间:2017-04-09 22:17:22

标签: mysql sql

I want to calculate the total sum of Invoice_Total as one Sum, not per row.

SELECT YEAR(Invoice_Date)as SalesYear, MONTH(Invoice_Date)as SalesMonth, Client_Name,Invoice_Total, SUM(Invoice_Total) AS TotalSales FROM INVOICE JOIN CLIENT ON CLIENT.Client_ID= INVOICE.Client_ID WHERE Invoice_Date between '2016-01-01'and'2016-01-31' GROUP BY Client_Name, Invoice_Date, Invoice_Total;

1 个答案:

答案 0 :(得分:0)

如果只是想要SUM作为一行,那么您可以从查询中删除GROUP BY,例如它可以很简单:

SELECT SUM(Invoice_Total)
FROM INVOICE
WHERE Invoice_Date BETWEEN '2016-01-01' AND '2016-01-31';

<强>更新

如果您希望现有查询中的SUM Invoice_Total(以及其他列),则可以将其包装到另一个查询中,例如:

SELECT SalesYear, SalesMonth, Client_Name, Invoice_Total, SUM(TotalSales) 
FROM (
 SELECT YEAR(Invoice_Date)as SalesYear, MONTH(Invoice_Date)as SalesMonth,   Client_Name,Invoice_Total, SUM(Invoice_Total) AS TotalSales FROM INVOICE
 JOIN CLIENT
 ON CLIENT.Client_ID= INVOICE.Client_ID
 WHERE Invoice_Date between '2016-01-01'and'2016-01-31'
 GROUP BY Client_Name, Invoice_Date, Invoice_Total
) a;

如果您不想要其他列(因为在所有行中打印相同的值没有多大意义),只需使用SELECT SUM(TotalSales) FROM...