如何通过一个mysql查询获得总和的总和

时间:2014-09-24 13:42:32

标签: mysql sum

我有以下查询,它让我回到发布商的所有分组销售

SELECT 
   publisher_name, 
   SUM(sales) AS sales, 
   SUM(comission) AS comission, 
   SUM(sale_total) AS publisher_total_sale
FROM reports_summary 
   GROUP BY publisher_name 
   ORDER BY publisher_total_sale DESC

输出

-------------------------------------------------------------------------------
+ publisher_name + | + sales + | + commission + | publisher_total_sale_count +
-------------------------------------------------------------------------------
+ some publisher + | 254,12  + | + 12,23      + | 15                         +
-------------------------------------------------------------------------------
+ publisher2     + | 123     + | + 23         + | 12                         +
-------------------------------------------------------------------------------

我想要什么

另一列在此示例中为sum(publisher_total_sale_count)提供了27。                          +
 有可能,我该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以在子查询中计算,然后使用交叉联接将其带入主查询:

SELECT 
   publisher_name, 
   SUM(sales) AS sales, 
   SUM(comission) AS comission, 
   SUM(sale_total) AS publisher_total_sale,
   sumOfTotalSales
FROM reports_summary,
   (SELECT Sum(Sales_total) as sumOfTotalSales FROM reports_summary) as subTotalSales
   GROUP BY publisher_name, sumOfTotalSales 
   ORDER BY publisher_total_sale DESC

要在GUI而不是SQL编辑器中执行此操作,您可以创建一个新查询,执行子查询执行的操作SELECT Sum(Sales_total) as sumOfTotalSales FROM reports_summary然后将其带入主查询并确保之间没有连接两张桌子。