1

SQLクエリで問題に直面しています。スキーマを添付しています。ここに画像の説明を入力

私が欲しいのは。以下のような結果を生成するクエリを生成したいと思います。

=======================================================================================
 showroom_id  |  total_salary  | total_expense |  total_sold | balance
=======================================================================================
|     1       |    2000        |   8000        |   30000     | 20000
=======================================================================================
|     2       |    1000        |   4000        |   25000     | 20000
=======================================================================================
|     3       |    3000        |   7000        |   30000     | 20000
====================================================================================

ショールーム ID でグループ化し、経費額、スタッフの給与、アイテムの価格を合計して、個々の行に表示したいと考えています。次に、別の列の残高が表示されtotal_sold -( total_expanse + total_salary)ます。どうすればクエリを実行できますか?

4

1 に答える 1

2
select
    s.id as showroom_id,
    sal.amount as total_salary,
    exp.amount as total_expense
    -- not sure where to get total_sold amount?
from showroom as s
    left outer join (
        select sum(t.amount) as salary, t.showroom_id
        from staff_salary as t
        group by t.showroom_id
   ) as sal on sal.showroom_id = s.id
    left outer join (
        select sum(t.amount) as salary, t.showroom_id
        from expense as t
        group by t.showroom_id
   ) as exp on exp.showroom_id = s.id
于 2013-10-14T19:33:39.143 に答える