+3  A: 

Use:

  SELECT oi.orderid,
         SUM(oi.quantity * p.price) AS grand_total,
    FROM ORDERITEM oi
    JOIN PRODUCT p ON p.id = oi.productid
   WHERE oi.orderid = @OrderId
GROUP BY oi.orderid

Mind that if either oi.quantity or p.price is null, the SUM will return NULL.

OMG Ponies
+1 for correctness - and ripping off your basic structure.
Randy
+1 However I think the statement "if either `oi.quantity` or `p.price` is null then SUM will return NULL" is a little unclear. Sum will only return null if the value for _all_ rows of a group is null. Otherwise sum ignores null. So quantity or price would have to be null for each row in a group for that group to be null.
Shannon Severance
Hey @OMG Ponies, your query returns a subtotal for each OrderItem row, rather than the grand total for all rows.. Almost like the SUM is being ignored :/
Marko
A: 
select orderID, sum(subtotal) as order_total from
(
    select orderID, productID, price, qty, price * qty as subtotal
    from product p inner join orderitem o on p.id = o.productID
    where o.orderID = @orderID
) t
group by orderID
Beth
Hi @Beth - I get a "Incorrect syntax near the keyword 'GROUP'."
Marko
sorry, needed to add an alias after the )
Beth
This returns every row in the OrderItem table, and the order_total and subtotal are identical, so SUM again isn't doing it's job :/
Marko
sorry again, didn't know you only wanted the grand total and I needed to remove columns from the inner query you don't want broken down.
Beth
+1  A: 

i think this - including null value = 0

 SELECT oi.id, 
         SUM(nvl(oi.quantity,0) * nvl(p.price,0)) AS total_qty 
    FROM ORDERITEM oi 
    JOIN PRODUCT p ON p.id = oi.productid 
   WHERE oi.orderid = @OrderId 
GROUP BY oi.id 
Randy
`NVL` is Oracle specific - COALESCE would be a better choice.
OMG Ponies
There is no reason to do COALESCE (or NVL) on each input. You could: `coalesce(sum(oi.quantity * p.price), 0) as total_qty`.
Shannon Severance
This doesn't return the Grand Total, but rather the subtotal for each OrderItem row, same as @OMG Ponies' answer above..
Marko
+1  A: 

I think this is along the lines of what you're looking for. It appears that you want to see the orderid, the subtotal for each item in the order and the total amount for the order.

select o1.orderID, o1.subtotal, sum(o2.UnitPrice * o2.Quantity) as order_total from
(
    select o.orderID, o.price * o.qty as subtotal
    from product p inner join orderitem o on p.ProductID= o.productID
    where o.orderID = @OrderId
)as o1
inner join orderitem o2 on o1.OrderID = o2.OrderID
group by o1.orderID, o1.subtotal
mpminnich
Hey @mpminnich, I just need the grand total for all OrderItems, so basically the SUM of Quantity*Price
Marko