2008年10月29日 星期三

Sum

-------
Select OrderID,ProductID,UnitPrice*Quantity*(1-Discount) as '小計'
from dbo.[Order Details]

-------
Select Convert(int,Sum(UnitPrice*Quantity*(1-Discount))) as '訂單總金額'
from dbo.[Order Details]

----
Select YEAR(OrderDate) as '年度' ,
Convert(int,Sum(UnitPrice*Quantity*(1-Discount))) as '訂單總金額'
from dbo.Orders O join dbo.[Order Details] D on O.OrderID=D.OrderID
GROUP BY YEAR(OrderDate)

------
Select YEAR(OrderDate) as '年度' ,Datepart(Quarter,OrderDate) as '季' ,
Convert(int,Sum(UnitPrice*Quantity*(1-Discount))) as '訂單總金額' ,
Count(distinct O.OrderID) as '訂單總筆數'
from dbo.Orders O join dbo.[Order Details] D on O.OrderID=D.OrderID
GROUP BY YEAR(OrderDate),Datepart(Quarter,OrderDate)
Order BY YEAR(OrderDate),Datepart(Quarter,OrderDate)

沒有留言: