Calculating the SUM of (Quantity*Price) from 2 different tables

前端 未结 5 1777
孤独总比滥情好
孤独总比滥情好 2021-02-04 15:45

I have two tables as follows

PRODUCT table

Id | Name | Price

And an ORDERITEM table

Id | Orde         


        
5条回答
  •  别跟我提以往
    2021-02-04 16:22

    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
    

提交回复
热议问题