我想知道特定订单ID的总价。这些信息包含在这些表格中:
Order_line_t
Order_ID Product_ID Quantity
-------- ---------- --------
1006 4 1
1006 5 2
1006 7 2
Uses_t
Product_ID Material_ID Footage
---------- ----------- -------
4 16 20
5 15 13
7 10 16
Raw_Materials_t
Material_ID Unit_Price
----------- ----------
16 05.70
15 16.72
10 15.55
总费用由
计算Multiplying SUM ( Order_line_t.Quantity * Uses_t.Footage * Raw_Materials_t.Unit_Price) AS Total
我当前的查询只返回错误:
SELECT Order_line_t.Order_ID, Order_line_t.Product_ID, Uses_t.Product_ID ,Uses_t.Footage ,
SUM ( Order_line_t.Quantity * Uses_t.Footage * Raw_Materials_t.Unit_Price) AS Total
FROM Order_line_t, Uses_t , Raw_Materials_t
WHERE Order_line_t.Order_ID = 1006;
帮助表示感谢!我究竟如何加入这些表?我错过了什么?
答案 0 :(得分:1)
首先,你错过了表之间的实际连接,其次是你错过了你的小组
SELECT Order_line_t.Order_ID,
Order_line_t.Product_ID,
Uses_t.Product_ID ,
Uses_t.Footage ,
SUM ( Order_line_t.Quantity * Uses_t.Footage * Raw_Materials_t.Unit_Price) AS Total
FROM Order_line_t INNER JOIN
Uses_t ON Order_line_t.Product_ID = Uses_t.Product_ID INNER JOIN
Raw_Materials_t ON Uses_t.Material_ID = Raw_Materials_t.Material_ID
WHERE Order_line_t.Order_ID = 1006;
GROUP BY Order_line_t.Order_ID,
Order_line_t.Product_ID,
Uses_t.Product_ID ,
Uses_t.Footage
修改强>
试试这个
SELECT Order_line_t.Order_ID,
Order_line_t.Product_ID,
Uses_t.Product_ID,
Uses_t.Footage,
Sum([Order_line_t].[Quantity]*[Uses_t].[Footage]*[Raw_Materials_t].[Unit_Price]) AS Total
FROM (Order_line_t INNER JOIN
Uses_t ON Order_line_t.Product_ID = Uses_t.Product_ID) INNER JOIN
Raw_Materials_t ON Uses_t.Material_ID = Raw_Materials_t.Material_ID
GROUP BY Order_line_t.Order_ID,
Order_line_t.Product_ID,
Uses_t.Product_ID,
Uses_t.Footage;
修改强>
按订单ID总计
SELECT Order_line_t.Order_ID,
Sum([Order_line_t].[Quantity]*[Uses_t].[Footage]*[Raw_Materials_t].[Unit_Price]) AS Total
FROM (Order_line_t INNER JOIN
Uses_t ON Order_line_t.Product_ID = Uses_t.Product_ID) INNER JOIN
Raw_Materials_t ON Uses_t.Material_ID = Raw_Materials_t.Material_ID
GROUP BY Order_line_t.Order_ID;
答案 1 :(得分:0)
我认为您需要计算每一行的成本,然后对整个订单求和。类似的东西:
select
Order_ID, sum(RowCost) as OrderCost
from (
select
o.Order_ID, o.Product_ID, o.Quantity,
u.Material_ID, u.Footage,
r.Unit_Price,
(o.Quantity * u.Footage * r.UnitPrice) as RowCost
from
order_line_t o INNER JOIN
uses_t u on o.Product_ID = u.Product_ID INNER JOIN
Raw_materials_t r on u.Material_ID = r.Material_ID
where
o.Order_ID = 1006
) z
group by
Order_ID