Sql 连接两个表中的值并将其值分组

Sql 连接两个表中的值并将其值分组,sql,oracle,Sql,Oracle,我有两张桌子 表1 NAME COMPONENT QUANTITY STATUS NAME1 COLUMN 10 READY NAME2 COLUMN 20 READY NAME3 COLUMN 15 NOTREADY NAME4 COLUMN 10 READY NAME5 BEAM 20 NOTREADY NAME6 BEAM 15 NOTREADY NAME7 BEAM

我有两张桌子

表1

NAME  COMPONENT QUANTITY STATUS
NAME1 COLUMN    10       READY
NAME2 COLUMN    20       READY
NAME3 COLUMN    15       NOTREADY
NAME4 COLUMN    10       READY
NAME5 BEAM      20       NOTREADY
NAME6 BEAM      15       NOTREADY
NAME7 BEAM      10       READY
NAME8 GIRTH     30       NOTREADY
NAME9 GIRTH     25       NOTREADY
表2:

NAME  PROCESS
NAME1 5
NAME2 7
NAME4 10
NAME7 8
因此,在这个软件流程中,当项目未准备就绪时,它不会出现在表2中。表2是另一个不同的过程。 我希望在我的输出中看到这样的东西

COMPONENT_RES COMP_READY COMP_NOT_READY TOTAL_PROCESS
COLUMN        40         15             22
BEAM          10         35             10
GIRTH         0          55             0
select distinct md.component COMPONENT_RES, sum(md.quantity) COMP_READY, 
(select sum(md.quantity) COMP_NOT_READY from table1 md where md.status = 'NOTREADY')
from table1 md where md.status = 'ACTIVE'
group by md.component
我的问题是这样的

COMPONENT_RES COMP_READY COMP_NOT_READY TOTAL_PROCESS
COLUMN        40         15             22
BEAM          10         35             10
GIRTH         0          55             0
select distinct md.component COMPONENT_RES, sum(md.quantity) COMP_READY, 
(select sum(md.quantity) COMP_NOT_READY from table1 md where md.status = 'NOTREADY')
from table1 md where md.status = 'ACTIVE'
group by md.component
我这里有两个问题, 1.我的查询无法区分COMP\u READY和COMP\u NOT\u READY 2.当周长不存在但必须显示为0时,我尝试了无数次从第二个表中集成TOTAL_过程


谢谢大家

您需要进行两次聚合并将结果合并在一起:

select c.component, c.comp_ready, c.comp_notready, cp.total_process
from (select component,
             sum(case when status = 'READY' then Quantity else 0 end) as Comp_Ready,
             sum(case when status = 'NOTREADY' then Quantity else 0 end) as Comp_NotReady,
      from table1
      group by component
     ) c left join
     (select t1.component, sum(t2.component) as total_process
      from table1 t1 join
           table2 t2
           on t1.name = t2.name
      group by t1.component
     ) cp
     on c.component = cp.component;

诀窍是第二个子查询需要一个
连接
,以便获得适当的组件。

您需要的是表之间的外部连接

查询1

select 
table1.component_,
sum(case when table1.status_ = 'READY' then table1.quantity else 0 end) comp_ready,
sum(case when table1.status_ = 'NOTREADY' then table1.quantity else 0 end) comp_notready,
sum(coalesce(table2.process_,0)) total_process
from table1 left outer join table2
on table1.name_ = table2.name_
group by table1.component_
| COMPONENT_ | COMP_READY | COMP_NOTREADY | TOTAL_PROCESS |
|------------|------------|---------------|---------------|
|     COLUMN |         40 |            15 |            22 |
|      GIRTH |          0 |            55 |             0 |
|       BEAM |         10 |            35 |             8 |

select 
table1.component_,
sum(case when table1.status_ = 'READY' then table1.quantity else 0 end) comp_ready,
sum(case when table1.status_ = 'NOTREADY' then table1.quantity else 0 end) comp_notready,
sum(coalesce(table2.process_,0)) total_process
from table1 left outer join table2
on table1.name_ = table2.name_
group by table1.component_
| COMPONENT_ | COMP_READY | COMP_NOTREADY | TOTAL_PROCESS |
|------------|------------|---------------|---------------|
|     COLUMN |         40 |            15 |            22 |
|      GIRTH |          0 |            55 |             0 |
|       BEAM |         10 |            35 |             8 |

令人惊叹的。。。谢谢Gordon@ChrisWeltes . . . 我认为你不会(毕竟,你接受了答案)。我认为否决票是恶意的——没有评论,答案似乎很有用,而且我看不出有什么明显的错误。它也很有效。。我的问题是,如何添加另一个表?这是否意味着我还需要一个左外耳道?视情况而定。如果只需要公共记录,请使用内部联接。如果还要包括不匹配的记录,请使用外部联接。