how to get sum of this column in sql server
i'm use below query for fetch my data with an ordernumber...
SELECT Details.Quantity,
Details.OrderNumber,
Orders.OrderDate,
Products.ProductName,
Products.UnitPrice ,
Details.Quantity*Products.UnitPri开发者_运维技巧ce as qprice
FROM Details
INNER JOIN Orders
ON Details.OrderNumber = Orders.OrderNumber
INNER JOIN Products
ON Details.ProductID = Products.ProductID
where Orders.OrderNumber='14195'
this query give me below result
now i want give sum of qprice column..
how can i do this??
Like this?
SELECT SUM(Details.Quantity * Products.UnitPrice) AS Amount
FROM Details
INNER JOIN Orders ON
Details.OrderNumber = Orders.OrderNumber
INNER JOIN Products ON
Details.ProductID = Products.ProductID
WHERE Orders.OrderNumber = '14195'
If you need to have TotalAmount as an extra column to what you already have then:
SELECT
Details.Quantity,
Details.OrderNumber,
Orders.OrderDate,
Products.ProductName,
Products.UnitPrice,
Details.Quantity*Products.UnitPrice as qprice,
SUM(Details.Quantity*Products.UnitPrice) OVER() TotalAmount
FROM Details
INNER JOIN Orders ON
Details.OrderNumber = Orders.OrderNumber
INNER JOIN Products ON
Details.ProductID = Products.ProductID
WHERE Orders.OrderNumber = '14195'
精彩评论