用于连接2个表的SQL查询

时间:2018-04-30 05:59:42

标签: sql join views

我这里有2个表,其中包含以下结构Table1,其中包含估计成本,Table2包含Table2中的实际成本和任务字段,是基于Table1预算的外键 任务字段,我需要编写一个查询和视图来获取以下结果表,它提供了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 |
+---------+------+-------------+

结果表:

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

5 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您可以在表1上执行左连接,在任务列上执行表2,并在 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 

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

希望这有帮助,谢谢。

答案 2 :(得分:0)

一种可能的解决方案是使用两个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

答案 3 :(得分:0)

您可以使用此查询:

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 |
+----------+----------------+-------------+

答案 4 :(得分:0)

试试这个

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