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

前端 未结 5 1823
孤独总比滥情好
孤独总比滥情好 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:03

    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.

提交回复
热议问题