2015-12-19 53 views
0

我試圖讓所有的關鍵所有年份(單獨)有超過一個在每年,從三個DB的takeng信息 我可以做一年,但如何從一切做起。實際上是兩個1996年和有ARA 1997年集團與額外的功能MySQL

這是怎麼得到的一切爲1996年和幾乎相同的如何獲得1997年 但如何加入他們ORR在這裏得到正確everithing

SELECT OrderID, CustomerID, OrderDate, sum(Cost) as TotalCost 
FROM (SELECT Orders.CustomerID, Orders.OrderID, Orders.OrderDate, Orders.OrderID, OrderDetails.ProductID, (OrderDetails.Quantity* Products.Price) as Cost 
      FROM Orders, OrderDetails, Products 
      WHERE Orders.OrderID = OrderDetails.OrderID 
      AND OrderDetails.ProductID= Products.ProductID) 
WHERE OrderDate<="1996-12-31" 
Group by CustomerID 

是表什麼我有 訂單

+---------+------------+------------+------------+-----------+ 
| OrderID | CustomerID | EmployeeID | OrderDate | ShipperID | 
+---------+------------+------------+------------+-----------+ 
| 10248 |   90 |   5 | 1996-07-04 |   3 | 
| 10249 |   81 |   6 | 1996-07-05 |   1 | 
| 10250 |   34 |   4 | 1996-07-08 |   2 | 
| 10251 |   84 |   3 | 1997-07-08 |   1 | 
| 10252 |   76 |   4 | 1997-07-09 |   2 | 
+---------+------------+------------+------------+-----------+ 

訂單明細

+---------------+---------+-----------+----------+ 
| OrderDetailID | OrderID | ProductID | Quantity | 
+---------------+---------+-----------+----------+ 
|    1 | 10248 |  1 |  12 | 
|    2 | 10248 |  2 |  10 | 
|    3 | 10248 |  3 |  5 | 
|    4 | 10249 |  1 |  9 | 
|    5 | 10249 |  2 |  40 | 
|    6 | 10250 |  1 |  10 | 
|    7 | 10250 |  2 |  35 | 
|    8 | 10250 |  3 |  15 | 
+---------------+---------+-----------+----------+ 

和產品

+-----------+--+------------+------------+--+-------+ 
| ProductID | | SupplierID | CategoryID | | Price | 
+-----------+--+------------+------------+--+-------+ 
|   1 | |   1 |   1 | | 18 | 
|   2 | |   1 |   1 | | 19 | 
|   3 | |   1 |   2 | | 10 | 
+-----------+--+------------+------------+--+-------+ 

我使用MySQL 對不起長期後

附:它說

沒有這樣的功能:一年

P.P.S.我用SQLlite

+0

有人說,沒有一年函數關係,可訂購日期colomn的類型錯誤 –

+0

「沒有這樣的功能:年「 - 這種錯誤消息格式是典型的sqlite,而不是mysql。你確定你使用mysql,而不是sqlite嗎? – Shadow

回答

1

使用一年​​()的MySQL函數從日期中提取年份:

select o.customerid, year(o.orderdate) as year, sum(od.Quantity* p.Price) as totalcost 
from orders o 
inner join orderdetails od on o.orderid=od.orderid 
inner join products p on od.productid=p.productid 
group by o.customerid, year(o.orderdate) 

更新:

根據錯誤消息,您可能正在使用sqlite而不是mysql。在這種情況下使用SUBSTR()函數來獲得4個最左邊的字符,否則的邏輯是一樣的:

select o.customerid, substr(o.orderdate,1,4) as year, sum(od.Quantity* p.Price) as totalcost 
from orders o 
inner join orderdetails od on o.orderid=od.orderid 
inner join products p on od.productid=p.productid 
group by o.customerid, substr(o.orderdate,1,4) 
2

您將包括在聚集year(OrderDate)

SELECT OrderID, CustomerID, year(OrderDate), sum(Cost) as TotalCost 
FROM (SELECT Orders.CustomerID, Orders.OrderID, Orders.OrderDate, Orders.OrderID, OrderDetails.ProductID, (OrderDetails.Quantity* Products.Price) as Cost 
     FROM Orders JOIN 
      OrderDetails 
      ON Orders.OrderID = OrderDetails.OrderID JOIN 
      Products 
      ON OrderDetails.ProductID = Products.ProductID 
    ) x 
WHERE OrderDate <= '1996-12-31' 
Group by CustomerID, year(OrderDate); 

其實,子查詢是不必要的,你應該使用表的別名:

SELECT o.CustomerID, year(o.OrderDate), SUM(od.Quantity * p.Price) as Cost 
FROM Orders o JOIN 
    OrderDetails od 
    ON o.OrderID = od.OrderID JOIN 
    Products p 
    ON od.ProductID = p.ProductID 
GROUP BY CustomerId, year(OrderDate) 
+0

1.你應該從外層選擇中刪除。 2.爲什麼你需要一個子查詢? – Shadow

+0

現在你已經發布了完全相同的解決方案,因爲我有:) – Shadow

+0

,但它表示沒有年份功能 –