Query help please?

Discussion in 'Programming' started by amaze, Mar 16, 2009.

  1. #1
    Hi,

    I currently have the query as below. However I want to work out how many units I have sold. To do this I need to multiply the od.ingredientid with od.quantity on a row per row basis, then the total of that is the amount of units sold.

    Is this possible in SQL?

    
    select od.fkingredientid, od.quantity
    from dorderdetails od, dordergroups og
    where og.pkordergroupid = od.fkordergroupid
    and od.fkingredientid = 391
    and og.deleted = 0
    
    Code (markup):
    #

    Thanks
     
    amaze, Mar 16, 2009 IP
  2. robhustle

    robhustle Peon

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Just do it in the query itself, ie:

    select a, b, a * b as product
    from table
     
    robhustle, Mar 17, 2009 IP
  3. amaze

    amaze Active Member

    Messages:
    594
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Ahhh I didn't know you can do that! :eek:

    I now have:

    select (od.quantity * od.amount) AS totalQTY
    from dorderdetails od, dordergroups og
    where og.pkordergroupid = od.fkordergroupid
    and od.fkingredientid = 391
    and og.deleted = 0
    Code (markup):
    What is the syntax to get the sum of totalQTY? Is that possible?

    Thanks
     
    amaze, Mar 17, 2009 IP
  4. robhustle

    robhustle Peon

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Not sure. Maybe:

    select a, b, a * b as product,
    (select sum(a*b) from table) as total
    from table

    Subselect seems inefficient. Also, you might check out using query of query. I use that all the time to operate on queries that have been returned.

    Anyone?
     
    robhustle, Mar 18, 2009 IP