Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2008
    Location
    Netherlands
    Posts
    58

    Question Unanswered: 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

  2. #2
    Join Date
    Jun 2007
    Posts
    197

    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;

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    muiter, your query doesn't work because it's missing the GROUP BY clause
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  4. #4
    Join Date
    Dec 2008
    Location
    Netherlands
    Posts
    58
    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.

  5. #5
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    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

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •