显示每年的收入

时间:2015-04-01 17:09:06

标签: mysql sql northwind

我正在使用NorthWind SQL数据库。对于那些不熟悉的人,它有以下几个表:

 Orders, 
 OrderDetails, 
 Products,  
 categories 
 (Orders.OrderID = OrderDetails.OrderID) 
 (Products.ProductID = OrderDetails.ProductID)
 (Categories.categoryID = Products.categoryID)

 Orders has a column named "OrderDate" (formatted as dd/mm/yy)
 OrderDetails has a column named "Price"

我需要显示总收入"每年。也就是所有"价格的总和"。知道怎么做吗?

1 个答案:

答案 0 :(得分:1)

尝试这样的事情,因为我们没有完整的表格结构。这种GROUP BY方式适用于MySQL

   SELECT SUM (OD.Price) AS Revenue FROM OrderDetails OD
    INNER JOIN Orders O ON Orders.OrderID = OrderDetails.OrderID
    GROUP BY YEAR(O.OrderDate);