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 > Sum more columns

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-23-11, 10:07
Muiter Muiter is offline
Registered User
 
Join Date: Dec 2008
Location: Netherlands
Posts: 56
Question Sum more columns

I have an query with 2 different SUM's. How can I calculate the total of all 3 in the query?

Code:
SELECT
SUM(inh_pr.prijs_verkoop) AS prijs_verkoop1,
SUM(IF(inh_plt.overrule > 0, inh_plt.overrule, inh_plt.verkoop_stk)) AS prijs_verkoop2,
SUM(inh_lntk.totaal_stk) AS prijs_verkoop3
FROM 3_product_folder AS fld
LEFT JOIN 0_calculatie_inh_id_geg_product AS inh_pr
ON inh_pr.calculatie_inh_id = fld.product_id
LEFT JOIN 0_calculatie_inh_id_geg_plaat AS inh_plt
ON inh_plt.calculatie_inh_id = fld.product_id
LEFT JOIN 0_calculatie_inh_id_geg_lntk_product AS inh_lntk
ON inh_lntk.calculatie_inh_id = fld.product_id
LEFT JOIN 2_folder AS fld2
ON fld2.id = fld.folder_id
Reply With Quote
  #2 (permalink)  
Old 10-24-11, 02:35
ankur02018 ankur02018 is offline
Registered User
 
Join Date: Jun 2007
Posts: 189
Smile

Quote:
Originally Posted by Muiter View Post
I have an query with 2 different SUM's. How can I calculate the total of all 3 in the query?

Code:
SELECT
SUM(inh_pr.prijs_verkoop) AS prijs_verkoop1,
SUM(IF(inh_plt.overrule > 0, inh_plt.overrule, inh_plt.verkoop_stk)) AS prijs_verkoop2,
SUM(inh_lntk.totaal_stk) AS prijs_verkoop3
FROM 3_product_folder AS fld
LEFT JOIN 0_calculatie_inh_id_geg_product AS inh_pr
ON inh_pr.calculatie_inh_id = fld.product_id
LEFT JOIN 0_calculatie_inh_id_geg_plaat AS inh_plt
ON inh_plt.calculatie_inh_id = fld.product_id
LEFT JOIN 0_calculatie_inh_id_geg_lntk_product AS inh_lntk
ON inh_lntk.calculatie_inh_id = fld.product_id
LEFT JOIN 2_folder AS fld2
ON fld2.id = fld.folder_id
Try This

Code:
SELECT (prijs_verkoop1 + prijs_verkoop2 + prijs_verkoop3) as Total from (above query) as a;
Reply With Quote
  #3 (permalink)  
Old 10-24-11, 05:49
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
muiter, your query doesn't work because it's missing the GROUP BY clause
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 10-24-11, 14:57
Muiter Muiter is offline
Registered User
 
Join Date: Dec 2008
Location: Netherlands
Posts: 56
Quote:
Originally Posted by ankur02018 View Post
Try This

Code:
SELECT (prijs_verkoop1 + prijs_verkoop2 + prijs_verkoop3) as Total from (above query) as a;
I have tried that but I can't get it to work in my query.

Quote:
Originally Posted by r937 View Post
muiter, your query doesn't work because it's missing the GROUP BY clause
It does work but to be complete I should probably use the GROUP BY clause. Should I add one ore more columns to the GROUP BY clause?

The question is how to calculate the total.
Reply With Quote
  #5 (permalink)  
Old 10-24-11, 15:08
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by Muiter View Post
It does work but to be complete I should probably use the GROUP BY clause. Should I add one ore more columns to the GROUP BY clause?
wait a moment, i have to apologize, i was too hasty, and after looking at your query more closely, it does not need a GROUP BY clause

ankur's solution is correct

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

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