If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Have a query but need the sum of a sum

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-20-11, 03:50
kiwimediapro kiwimediapro is offline
Registered User
 
Join Date: May 2011
Posts: 13
Exclamation Have a query but need the sum of a sum

Hello all,

I have this query so far:


SELECT SUM(t1.totalprice) as totalprice,
SUM(t1.sales) as sales, t1.`year` as `year`,
t1.mnth as `month`
from
(SELECT
Sum(crm_sales.totalprice) AS totalprice,
MONTHNAME(crm_sales.date) AS mnth,
YEAR(crm_sales.date) AS `year`,
Count(crm_sales.totalprice) AS sales,
MONTH(crm_sales.date) AS `ordering`
FROM `crm_sales`
WHERE `date` BETWEEN '2009-01-01' AND '2010-05-28'
GROUP BY crm_sales.date
) as t1
GROUP BY t1.`year`,t1.mnth
ORDER BY t1.`year`, t1.ordering

I need all these fields but also the sum of the sum of the field indicated:

SUM(t1.totalprice) as totalprice
SUM(t1.sales) as sales <------------ SUm of this sum
t1.`year` as `year`
t1.mnth as `month`

Any ideas?
Reply With Quote
  #2 (permalink)  
Old 05-20-11, 04:11
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
if i understand what you're asking, try adding WITH ROLLUP to your GROUP BY clause

otherwise, you haven't explained the problem clearly enough
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 05-20-11, 04:42
kiwimediapro kiwimediapro is offline
Registered User
 
Join Date: May 2011
Posts: 13
Does not seem work

SELECT SUM(t1.totalprice) as totalprice,
SUM(t1.sales) as salessubtotal,SUM(SUM(t1.sales) as salessubtotal) as salestotal, t1.`year` as `year`,
t1.mnth as `month`
from
(SELECT
Sum(crm_sales.totalprice) AS totalprice,
MONTHNAME(crm_sales.date) AS mnth,
YEAR(crm_sales.date) AS `year`,
Count(crm_sales.totalprice) AS sales,
MONTH(crm_sales.date) AS `ordering`
FROM `crm_sales`
WHERE `date` BETWEEN '2009-01-01' AND '2010-05-28'
GROUP BY crm_sales.date
) as t1
GROUP BY t1.`year`,t1.mnth WITH ROLLUP
ORDER BY t1.`year`, t1.ordering
Reply With Quote
  #4 (permalink)  
Old 05-20-11, 07:44
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by kiwimediapro View Post
SUM(SUM(t1.sales)
you're not allowed to do that (even if you had closed the parentheses properly
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 05-20-11, 16:16
kiwimediapro kiwimediapro is offline
Registered User
 
Join Date: May 2011
Posts: 13
Ok so how does one do this then?

Thanks for that but any ideas the non how to achieve this.

Cheers
Reply With Quote
  #6 (permalink)  
Old 05-20-11, 17:58
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,606
The obvious answer would be to do the arithmatic on the client side, in the reporting software.

SQL is intended to be a query language. It does aggregation, but only at one level (specified by the GROUP BY clause).

If you need more sophisticated processing, you really ought to use an application server like Microsoft SQL Reporting Services, Cognos, or WebFocus or a client side tool like Microsoft Access or Crystal Reports. These tools are built to handle this kind of problem cleanly and easily.

What you want can be done using just SQL syntax, but it is very ugly and difficult to maintain. That difficulty makes it a very poor business choice.

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
Reply

Tags
sum

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On