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 回答