2

特定の注文 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;

助けてください!これらのテーブルを正確に結合するにはどうすればよいですか? 私は何が欠けていますか?

4

2 に答える 2

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;
于 2012-11-01T04:04:27.603 に答える
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
于 2012-11-01T04:28:49.433 に答える