我想计算特定月份的总费用,我的代码是生成当月购买的商品,但是它计算的是所有商品的费用,而不仅仅是那个月
set dateformat 'dmy'
Select OrderID, OrderDate, UnitPrice
From SupplierOrder
Where OrderDate between '01/07/2015' AND '31/07/2015'
group by OrderID, OrderDate, UnitPrice
SELECT SUM(UnitPrice) AS TotalCostOfItems FROM SupplierOrder;
答案 0 :(得分:0)
我有点困惑为什么你有两个问题。无论如何,第一个是过滤日期,但第二个不是 - 因此第二个查询是计算所有订单的总计单价。要使第二个查询起作用,只需添加与第一个
相同的过滤器SELECT SUM(UnitPrice) AS TotalCostOfItems FROM SupplierOrder Where OrderDate between '01/07/2015' AND '31/07/2015';
答案 1 :(得分:0)
你应该在选择后添加where语句。
SELECT SUM(UnitPrice) AS TotalCostOfItems FROM SupplierOrder
Where OrderDate between '01/07/2015' AND '31/07/2015'
希望这个帮助
答案 2 :(得分:0)
试试这个
select mnth, yr, sum(unitprice) unitprice
from
(
SELECT
DATEPART(month,orderdate) mnth,
datepart(year,orderdate) yr,
unitprice
from supplierorder
) a
group by mnth, yr