Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. MySQL
  4. sum from two tables

sum from two tables

Scheduled Pinned Locked Moved MySQL
databasehelp
7 Posts 4 Posters 88 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abushahin
    wrote on last edited by
    #1

    Hey there, I'm trying to use sum from two tables, but when I get the result its multiplying the sum by two for some reason. This is my query: select sum(credit.amount),sum(payment.amount) from payment, credit where cust_id=9. This is returning the sums but multiplied twice, any help appreciated, abu

    http://abushahin.eu

    A L S 3 Replies Last reply
    0
    • A abushahin

      Hey there, I'm trying to use sum from two tables, but when I get the result its multiplying the sum by two for some reason. This is my query: select sum(credit.amount),sum(payment.amount) from payment, credit where cust_id=9. This is returning the sums but multiplied twice, any help appreciated, abu

      http://abushahin.eu

      A Offline
      A Offline
      April Fans
      wrote on last edited by
      #2

      Because you use sum from two tables, the returning was caculated twice. Try the following code: select sum(credit.amount) from credit where cust_id=9 select sum(payment.amount) from payment where cust_id=9

      April Comm100 - Leading Live Chat Software Provider

      1 Reply Last reply
      0
      • A abushahin

        Hey there, I'm trying to use sum from two tables, but when I get the result its multiplying the sum by two for some reason. This is my query: select sum(credit.amount),sum(payment.amount) from payment, credit where cust_id=9. This is returning the sums but multiplied twice, any help appreciated, abu

        http://abushahin.eu

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        It is probably because you are not joining the two tables based on a common key, and if there is no common key, then you should rewrite the query as two separate queries.

        1 Reply Last reply
        0
        • A abushahin

          Hey there, I'm trying to use sum from two tables, but when I get the result its multiplying the sum by two for some reason. This is my query: select sum(credit.amount),sum(payment.amount) from payment, credit where cust_id=9. This is returning the sums but multiplied twice, any help appreciated, abu

          http://abushahin.eu

          S Offline
          S Offline
          slam Iqbal
          wrote on last edited by
          #4

          Try this:

          select sum(credit.amount) credit_amount,0 payment_amount from credit where cust_id=9
          UNION
          select 0 credit_amount, sum(payment.amount) payment_amount from payment where cust_id=9

          Try Another one:

          Select * from amounts
          (
          select sum(credit.amount) credit_amount,0 payment_amount from credit where cust_id=9
          UNION
          select 0 credit_amount, sum(payment.amount) payment_amount from payment where cust_id=9
          ) amounts

          Don't try to Join and group by command inside those 2 subquerys. Try it outside. That could arise some thing horrible Things.

          A 1 Reply Last reply
          0
          • S slam Iqbal

            Try this:

            select sum(credit.amount) credit_amount,0 payment_amount from credit where cust_id=9
            UNION
            select 0 credit_amount, sum(payment.amount) payment_amount from payment where cust_id=9

            Try Another one:

            Select * from amounts
            (
            select sum(credit.amount) credit_amount,0 payment_amount from credit where cust_id=9
            UNION
            select 0 credit_amount, sum(payment.amount) payment_amount from payment where cust_id=9
            ) amounts

            Don't try to Join and group by command inside those 2 subquerys. Try it outside. That could arise some thing horrible Things.

            A Offline
            A Offline
            abushahin
            wrote on last edited by
            #5

            @kisukortechai, thanks for your answer, whats the 0 stand for please?

            abushahin hadith

            S 1 Reply Last reply
            0
            • A abushahin

              @kisukortechai, thanks for your answer, whats the 0 stand for please?

              abushahin hadith

              S Offline
              S Offline
              slam Iqbal
              wrote on last edited by
              #6

              U can use NULL or '' insted of 0. it is to create an column which will let u to UNION 2 queries. otherwise u'll face some thing like this: With Extra Column( 0 was responsible for this) Client_amout, Pay_Amout 455,0 0,3454 0,565 565,0 No Extra Column( 0 wasn't) amout 455 3454 565 565 What is client_amount or what is pay_amount? Can u determine? I'm giving an examp1e query that i used in my IMS project:

              --with max_price, only for browsing items duplicating
              Create or Replace View V_ITEM_STOCK
              AS
              SELECT DERIVEDTBL_2.ITEMID, ITEM_2.ITEMNAME, ITEM_2.MODEL, ITEM_2.DESCRIPTION, ITEM_2.STORETYPE, ITEM_2.UNITPRICE,
              DERIVEDTBL_3.MAX_PRICE, DERIVEDTBL_2.TOTAL_P, DERIVEDTBL_2.TOTAL_S,
              DERIVEDTBL_2.TOTAL_P - DERIVEDTBL_2.TOTAL_S AS REMAINING
              FROM (SELECT ITEMID, SUM(TOTAL_P) AS TOTAL_P, SUM(TOTAL_S) AS TOTAL_S
              FROM (SELECT ITEM.ID AS ITEMID, DECODE(SUM(PURCHASEDETAILS.QUANTITY), NULL, 0, SUM(PURCHASEDETAILS.QUANTITY)) AS TOTAL_P,
              0 AS TOTAL_S
              FROM ITEM LEFT OUTER JOIN
              PURCHASEDETAILS ON ITEM.ID = PURCHASEDETAILS.ITEMID
              WHERE (ITEM.ID <> '-999')
              GROUP BY ITEM.ID
              UNION
              SELECT ITEM_1.ID AS ITEMID, 0 AS TOTAL_P, DECODE(SUM(SALESDETAILS.QUANTITY), NULL, 0, SUM(SALESDETAILS.QUANTITY))
              AS TOTAL_S
              FROM ITEM ITEM_1 LEFT OUTER JOIN
              SALESDETAILS ON ITEM_1.ID = SALESDETAILS.ITEMID
              WHERE (ITEM_1.ID <> '-999')
              GROUP BY ITEM_1.ID) DERIVEDTBL_1
              GROUP BY ITEMID) DERIVEDTBL_2 INNER JOIN
              ITEM ITEM_2 ON DERIVEDTBL_2.ITEMID = ITEM_2.ID INNER JOIN
              (SELECT ITEMID, MAX(MAX_PRICE) AS MAX_PRICE
              FROM (SELECT ITEMID, MAX(RATE) AS MAX_PRICE
              FROM SAL

              A 1 Reply Last reply
              0
              • S slam Iqbal

                U can use NULL or '' insted of 0. it is to create an column which will let u to UNION 2 queries. otherwise u'll face some thing like this: With Extra Column( 0 was responsible for this) Client_amout, Pay_Amout 455,0 0,3454 0,565 565,0 No Extra Column( 0 wasn't) amout 455 3454 565 565 What is client_amount or what is pay_amount? Can u determine? I'm giving an examp1e query that i used in my IMS project:

                --with max_price, only for browsing items duplicating
                Create or Replace View V_ITEM_STOCK
                AS
                SELECT DERIVEDTBL_2.ITEMID, ITEM_2.ITEMNAME, ITEM_2.MODEL, ITEM_2.DESCRIPTION, ITEM_2.STORETYPE, ITEM_2.UNITPRICE,
                DERIVEDTBL_3.MAX_PRICE, DERIVEDTBL_2.TOTAL_P, DERIVEDTBL_2.TOTAL_S,
                DERIVEDTBL_2.TOTAL_P - DERIVEDTBL_2.TOTAL_S AS REMAINING
                FROM (SELECT ITEMID, SUM(TOTAL_P) AS TOTAL_P, SUM(TOTAL_S) AS TOTAL_S
                FROM (SELECT ITEM.ID AS ITEMID, DECODE(SUM(PURCHASEDETAILS.QUANTITY), NULL, 0, SUM(PURCHASEDETAILS.QUANTITY)) AS TOTAL_P,
                0 AS TOTAL_S
                FROM ITEM LEFT OUTER JOIN
                PURCHASEDETAILS ON ITEM.ID = PURCHASEDETAILS.ITEMID
                WHERE (ITEM.ID <> '-999')
                GROUP BY ITEM.ID
                UNION
                SELECT ITEM_1.ID AS ITEMID, 0 AS TOTAL_P, DECODE(SUM(SALESDETAILS.QUANTITY), NULL, 0, SUM(SALESDETAILS.QUANTITY))
                AS TOTAL_S
                FROM ITEM ITEM_1 LEFT OUTER JOIN
                SALESDETAILS ON ITEM_1.ID = SALESDETAILS.ITEMID
                WHERE (ITEM_1.ID <> '-999')
                GROUP BY ITEM_1.ID) DERIVEDTBL_1
                GROUP BY ITEMID) DERIVEDTBL_2 INNER JOIN
                ITEM ITEM_2 ON DERIVEDTBL_2.ITEMID = ITEM_2.ID INNER JOIN
                (SELECT ITEMID, MAX(MAX_PRICE) AS MAX_PRICE
                FROM (SELECT ITEMID, MAX(RATE) AS MAX_PRICE
                FROM SAL

                A Offline
                A Offline
                abushahin
                wrote on last edited by
                #7

                Many thanks

                abushahin hadith

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups