我的程序有问题,因为我不知道如何计算单个年份的订单数量
CREATE PROCEDURE Orders @id VARCHAR(10)
AS
SELECT unitprice * quantity Amount,
Count([order details].orderid),
Year(orderdate)
FROM [order details]
INNER JOIN orders
ON [order details].orderid = orders.orderid
WHERE customerid = @id
GROUP BY unitprice * quantity,
Year(orderdate),
[order details].orderid
我必须按年份分组,但是我不知道该怎么做。请帮助我某人:)
答案 0 :(得分:1)
这是您想要的吗?
SELECT YEAR(o.OrderDate) as yyyy,
SUM(od.UNITPRICE*od.QUANTITY) as Amount,
count(*) as num_orders
FROM [Order Details] od INNER JOIN
Orders o
on od.OrderID = o.OrderID
WHERE o.CustomerID = @id
GROUP BY Year(OrderDate);
注意:
SELECT
中的所有列提供别名,这样您就知道它们应该在做什么。GROUP BY
中唯一的表达式应该是SELECT
中的未聚合列。