简体   繁体   中英

How to get the sum of the sum with one mysql query

I have the following query which gives me back all grouped sales on publisher

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

which outputs

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

what I want

another column which gives me sum(publisher_total_sale_count) in this example 27. +
Is it possible, how would I do this?

You can calculate that in a subquery then use a cross join to bring it into your main query:

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

To do this in the GUI instead of the SQL editor, you can create a new query that does what the subquery here is doing SELECT Sum(Sales_total) as sumOfTotalSales FROM reports_summary then bring that into your main query and insure that there is no join between the two tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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