2017-01-21 16 views
-1

私はこのクエリを持っているので、最初のクエリにwhere句(WHERE order_number = 1)を追加する必要がありますが、それは機能しません。内部結合のWHERE句はどこに追加しますか?

SELECT 
    last_name, 
    first_name, 
    total, 
    pickup_date, 
    MPU.salary, 
    MTU.materials, 
    OFU.otherfee 
FROM 
    finished AS FD 
     INNER JOIN 
    (SELECT 
     order_number, (SUM(salary * hours_worked)) AS 'salary' 
    FROM 
     manpower_used 
    GROUP BY order_number) AS MPU ON FD.order_number = MPU.order_number 
     INNER JOIN 
    (SELECT 
     order_number, (SUM(qty * price)) AS 'materials' 
    FROM 
     materials_used 
    GROUP BY order_number) AS MTU ON FD.order_number = MTU.order_number 
     INNER JOIN 
    (SELECT 
     order_number, 
      (SUM(overtime_fee + stair_fee + toll_fee)) AS 'otherfee' 
    FROM 
     other_fees 
    GROUP BY order_number) AS OFU ON FD.order_number = OFU.order_number 

ありがとう!

+1

where句などの終わりにそうあなたの質問の最後に、FD.order_number = MPU.order_numberとfd.order_number = 1' –

+0

という条件があります。 – kritikaTalwar

答えて

0

最後に。日付を比較するための

SELECT 
    last_name, 
    first_name, 
    total, 
    pickup_date, 
    MPU.salary, 
    MTU.materials, 
    OFU.otherfee 
FROM 
    finished AS FD 
     INNER JOIN 
    (SELECT 
     order_number, (SUM(salary * hours_worked)) AS 'salary' 
    FROM 
     manpower_used 
    GROUP BY order_number) AS MPU ON FD.order_number = MPU.order_number 
     INNER JOIN 
    (SELECT 
     order_number, (SUM(qty * price)) AS 'materials' 
    FROM 
     materials_used 
    GROUP BY order_number) AS MTU ON FD.order_number = MTU.order_number 
     INNER JOIN 
    (SELECT 
     order_number, 
      (SUM(overtime_fee + stair_fee + toll_fee)) AS 'otherfee' 
    FROM 
     other_fees 
    GROUP BY order_number) AS OFU ON FD.order_number = OFU.order_number 
    Where FD.order_number = 1 -- here 

年と月のため、あなたがそうのような別のフィルタを追加することができます(pickup_dateが言う):あなたはあなたのクエリ内の集計関数を使用していた

AND date_format(pickup_date,'%m-%Y') = date_format(curdate(),'%m-%Y') 
+0

最後のご質問ありがとう、今月のデータと今年だけのデータを取得する方法はありますか? – Aaron

+0

@Aaron回答が – GurV

-2

、つまり、なぜWHEREはしません作業。 WHEREの代わりにHAVINGキーワードを使用する必要があります。 HAVINGを使ってみてください。それがあなたのために働くことを願っています。

+0

で更新されています。グループバイ句の後にフィルタ条件が必要な場合のみ、ここでグループ化すると集計してからテーブルとして使用します。 –

0

前にして、グループそれはない集計フィールド上にある場合Where句、

Select [stuff] 
From [tables and joins] 
Where [Filter predicates] 
group by [grouping expressions] 

など:HavingとしてGROUP BY句の後

Select s.storeId, count(*) 
from stores s join storeproducts sp 
    on sp.storeId = s.storeId 
Where s.State = 'CA' 
group by s.storeId 

それは、集計関数を含む場合

Select [stuff] 
From [tables and joins] 
group by [grouping expressions] 
Having [filter predicates on aggregate expressions] 

like:

あなたのケースで
Select s.storeId, count(*) 
from stores s join storeproducts sp 
    on sp.storeId = s.storeId 
group by s.storeId 
having count(*) > 12 

、何group byは外部クエリに存在しない、エンド `..Where fd.order_number = 1 'に、または任意の' ONに

SELECT last_name, first_name, total, 
    pickup_date, MPU.salary, MTU.materials, OFU.otherfee 
FROM finished f 
    JOIN (SELECT order_number, 
      (SUM(salary * hours_worked)) 'salary' 
      FROM manpower_used 
      GROUP BY order_number) n 
     ON n.order_number = f.order_number 
    JOIN (SELECT order_number, SUM(qty * price) 'materials' 
      FROM materials_used 
      GROUP BY order_number) t 
     ON t.order_number = f.order_number 
    JOIN (SELECT order_number, 
      SUM(overtime_fee + stair_fee + toll_fee) 'otherfee' 
      FROM other_fees 
      GROUP BY order_number) o 
     ON o.order_number = f.order_number 
Where f.order_number = 1 -- here 
関連する問題