我试图获得所有Key的总和对于所有年份(单独),每年都有不止一个,并且来自三个DB的takeng信息 我可以做一年,但如何做到这一点。实际上有1996年和1997年的两年
这是如何获得1996年的一切,几乎与1997年相同 但是如何加入他们或正确地获得正确的
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.S。它说
没有这样的功能:年
P.P.S。我曾经使用过SQLlite
答案 0 :(得分: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)
答案 1 :(得分:1)
使用year()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)