SQL 连接 2 个表的查询

SQL Query for Joining 2 tables

我这里有 2 个 table 具有以下结构 表 1 包含估计成本,表 2 包含实际成本,表 2 中的任务字段是基于表 1 预算的外键 任务字段,我需要编写查询和视图以获得以下结果 table,它为我提供了 Estimated_Cost 的总和和 Actual_cost 类别详细信息的总和。

表 1:

+------+----------+----------------+
| Task | Category | Estimated_Cost |
+------+----------+----------------+
|    1 |     9100 |         100.00 |
|    2 |     9100 |          15.00 |
|    3 |     9100 |           6.00 |
|    4 |     9200 |           8.00 |
|    5 |     9200 |          11.00 |
+------+----------+----------------+

表 2:

+---------+------+-------------+
| Voucher | Task | Actual_Cost |
+---------+------+-------------+
|       1 |    1 |       10.00 |
|       2 |    1 |       20.00 |
|       3 |    1 |       15.00 |
|       4 |    2 |       32.00 |
|       5 |    4 |        8.00 |
|       6 |    5 |        3.00 |
|       7 |    5 |        4.00 |
+---------+------+-------------+

结果table:

+----------+----------------+-------------+
| Category | Estimated_Cost | Actual_Cost |
+----------+----------------+-------------+
|     9100 |         121.00 |       77.00 |
|     9200 |          19.00 |       15.00 |
+----------+----------------+-------------+

您可以在 table 1 和 table 2 的任务列 上进行 left join,并借助 group by子句,根据类别计算成本总和。下面是查询

只需一次查询即可帮助您实现目标。

select Category,sum(distinct(Estimated_Cost)),sum(Actual_Cost)
from Table1 t1 left join Table2 t2
on t1.task = t2.task
groupd by t1.Category 

只是想知道即使任务不是 table 2 的一部分,我们是否仍应在估算成本中显示总和?

希望对您有所帮助,谢谢。

一种可能的解决方案是使用两个group by sql命令,然后根据category

将它们连接起来
select te.category, sum_estimated_cost, sum_actual_cost
from (
    select 
    select category,
           sum(Estimated_Cost) sum_estimated_cost
    from Table1 t1
    group by t1.category 
) te
join
(
    select category, 
           sum(Actual_Cost) sum_actual_cost
    from Table1 t1, Table2 t1
    where t1.task = t2.task
    groupy by t1.category 
) ta on te.category = ta.category

您可以使用这个查询:

select x.Category Category, y.Estimated_Cost Estimated_Cost, x.Actual_Cost Actual_Cost 
from
(
  select t1.Category Category, sum(a.Actual_Cost) Actual_Cost
  from Actuals a,
  (select distinct Category from Budget) t1
  where a.Task IN (Select Task from Budget where Category = t1.Category)
  group by t1.Category
) x,
(
  select Category, sum(Estimated_Cost) Estimated_Cost
  from Budget
  group by Category
) y
where x.Category = y.Category;

插图与提供的数据:

select * from Budget;
+------+----------+----------------+
| Task | Category | Estimated_Cost |
+------+----------+----------------+
|    1 |     9100 |         100.00 |
|    2 |     9100 |          15.00 |
|    3 |     9100 |           6.00 |
|    4 |     9200 |           8.00 |
|    5 |     9200 |          11.00 |
+------+----------+----------------+

select * from Actuals;
+---------+------+-------------+
| Voucher | Task | Actual_Cost |
+---------+------+-------------+
|       1 |    1 |       10.00 |
|       2 |    1 |       20.00 |
|       3 |    1 |       15.00 |
|       4 |    2 |       32.00 |
|       5 |    4 |        8.00 |
|       6 |    5 |        3.00 |
|       7 |    5 |        4.00 |
+---------+------+-------------+
 select x.Category Category, y.Estimated_Cost Estimated_Cost, x.Actual_Cost Actual_Cost
  -> from
  -> (
  ->   select t1.Category Category, sum(a.Actual_Cost) Actual_Cost
  ->   from Actuals a,
  ->   (select distinct Category from Budget) t1
  ->   where a.Task IN (Select Task from Budget where Category = t1.Category)
  ->   group by t1.Category
  -> ) x,
  -> (
  ->   select Category, sum(Estimated_Cost) Estimated_Cost
  ->   from Budget
  ->   group by Category
  -> ) y
  -> where x.Category = y.Category;
+----------+----------------+-------------+
| Category | Estimated_Cost | Actual_Cost |
+----------+----------------+-------------+
|     9100 |         121.00 |       77.00 |
|     9200 |          19.00 |       15.00 |
+----------+----------------+-------------+
select cat as Category,SUM(ecost) as Estimated_Cost,SUM(acost) as Actual_Cost from (SELECT table1.Estimated_Cost as ecost,table1.Category as cat,table2.Task,SUM(table2.Actual_Cost) as acost FROM `table2` join table1 ON
table2.Task = table1.task
GROUP by table2.Task)
as t11 
GROUP by cat

试试这个

select at1.Category, at1.Estimated_Cost, at2.Actual_Cost from (
select t1.Category, sum(t1.Estimated_Cost) as Estimated_Cost from Table1 t1
group by t1.Category) at1
left join (
select t1.Category, sum(t2.Actual_Cost) as Actual_Cost from Table2 t2 join Table1 t1 on t2.Task = t1.Task
group by t1.Category) at2
on at1.Category = at2.Category