I have two tables as follows
PRODUCT
table
Id | Name | Price
And an ORDERITEM
table
Id | OrderId | ProductId | Quantity
What I'm trying to do is, calculate the subtotal price for each product (Quantity*Price) then SUM the TOTAL value for the entire order..
I'm trying something like this
SELECT Id, SUM(Quantity * (select Price from Product where Id = Id)) as qty
FROM OrderItem o
WHERE OrderId = @OrderId
But of course that doesn't work :)
Any help appreciated!
EDIT: I only want to show the grand total for the entire order, so basically the sum of Quantity*Price for every row in OrderItem. Here's some sample data.
Sample Data
TABLE Product
Id Name Price
1 Tomatoes 20.09
4 Cucumbers 27.72
5 Oranges 21.13
6 Lemons 20.05
7 Apples 12.05
Table OrderItem
Id OrderId ProductId Quantity
151 883 1 22
152 883 4 11
153 883 5 8
154 883 6 62
M
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.
i think this - including null value = 0
I had the same problem as Marko and come across a solution like this:
And just simply use the GetGrandTotal Stored Procedure to retrieve the Grand Total :)
Use:
Mind that if either
oi.quantity
orp.price
is null, the SUM will return NULL.