Sum

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • OuTCasT
    Contributor
    • Jan 2008
    • 374

    Sum

    how does one SUM up 2 columns and multiply it with another column.

    [CODE=sql]
    Select Sum(sellingpric e-buyingprice)*qu antity from orders where date = '2008/01/30'
    [/CODE]

    When i do this it gives me an error, telling me that the orders.quantity is not in an aggregate function or not in a GROUP BY clause

    when i change the statement to

    [CODE=sql]
    Select Sum(sellingpric e-buyingprice)*qu antity from orders where date = '2008/01/30' GROUP BY sellingprice
    [/CODE]

    then it doesnt sum up the totals.......it gives the totals for each row......
    i want the sum of the entire column

    please HELP....
  • Delerna
    Recognized Expert Top Contributor
    • Jan 2008
    • 1134

    #2
    try this
    Code:
    SELECT Sum((sellingprice-buyingprice)*quantity) 
    FROM orders 
    WHERE date = '2008/01/30'


    the problem with your first attempt
    Code:
    SELECT Sum(sellingprice-buyingprice)*quantity 
    FROM orders 
    WHERE date = '2008/01/30'
    is you had an agregate funtion on the two field (sellingprice-buyingprice)
    but no aggregate function on the field quantity

    every field must either be in an agregate function or it must be in the group by clause

    Comment

    • OuTCasT
      Contributor
      • Jan 2008
      • 374

      #3
      Originally posted by Delerna
      try this
      Code:
      SELECT Sum((sellingprice-buyingprice)*quantity) 
      FROM orders 
      WHERE date = '2008/01/30'


      the problem with your first attempt
      Code:
      SELECT Sum(sellingprice-buyingprice)*quantity 
      FROM orders 
      WHERE date = '2008/01/30'
      is you had an agregate funtion on the two field (sellingprice-buyingprice)
      but no aggregate function on the field quantity

      every field must either be in an agregate function or it must be in the group by clause
      Good stuff, i see i was missing the brackets which i guess add;s the quanity field to the aggregrate function.

      So last what i did was i put 2 sqlCommands into variables and then just multiplied the variables by each other.
      But now i will do it the way that u have done it, pretty simple
      Thanks

      Comment

      Working...