2011-12-20 7 views
0

私は2つのテーブルを持っています。価格、code_one 別のテーブルの値でグループ化

  • 第二表はしています:code_one、
  • code_two両方のテーブルがcode_oneテーブルを使用して参加することができ

    • 最初のテーブルには列があります。

      「価格の合計をcode_twoでグループ化する」方法

      私が試した:

      Select sum(price) from table1 Group By (select a.code_two from table2 a, table1 b where a.code_one = b.code_one) 
      
    +0

    @OMGポニーを:私は...グループ化した後、サブクエリを持つような何かを試してみました:GROUP BYが(、table2のからTABLE1をa.code_twoを選択b b.code_one = a.code_two) – Nik

    答えて

    1
    select code_two, SUM(price) 
    from table1 t1 INNER JOIN table2 t2 ON t1.code_one = t2.code_one 
    group by code_two 
    
    +0

    table1にwhere条件がある場合はどうなりますか?日付フィールドと同じです。だから私は特定の日付だけを欲しい。次は正しいですか?select code_two、SUM(price)from table1 t1 INNER JOIN table2 t2 ON t1.code_one = t2.code_oneここで、t1.date = 'somedate' group by code_two ??? – Nik

    +0

    はい、それは正しいです – Nighil

    +0

    Nighilは言ったように、はいそれは正しいです。 – Zohaib

    0
    select sum(price) 'Price' from firsttable inner join secondtable on firsttable.code_one = secondtable.code_one 
    group by secondtable.code_two