oracle 进行一个没有空寄存器的查询

pvcm50d1  于 2023-06-22  发布在  Oracle
关注(0)|答案(3)|浏览(110)

我有麻烦放在一起查询,并希望社会的帮助。我有以下场景:2个表(供应商和服务)。进行以下选择:

select f.supplier_id, s.planned_value, s.executed_value
from supplier f
inner join service s on f.id_supplier = s.id_supplier

我有以下结果:
| 供应商ID|计划值|EXECUTED_VALUE|
| - -----|- -----|- -----|
| 1|一百|一百|
| 2|两百|两百|
| 2|三百|零|
我需要设置一个查询,只有当来自该供应商的所有服务的performed value列都被填充时,它才返回供应商记录,没有重复,也就是说,在上面的场景中,结果应该是:
| 供应商ID|供应商名称|
| - -----|- -----|
| 1| ABC|
当第三行为null时,响应应该是:
| 供应商ID|供应商名称|
| - -----|- -----|
| 1| ABC|
| 2| DEF|
感谢所有能支持的人
我试着用计数,没有成功

23c0lvtd

23c0lvtd1#

这里有一个选择:
样本数据:

SQL> with
  2  supplier (id_supplier, supplier_name) as
  3    (select 1, 'ABC' from dual union all
  4     select 2, 'DEF' from dual
  5    ),
  6  service (id_supplier, planned_Value, executed_value) as
  7    (select 1, 100, 100  from dual union all
  8     select 2, 200, 200  from dual union all
  9     select 2, 300, null from dual
 10    )

查询从这里开始:

11  select f.id_supplier, f.supplier_name
 12  from supplier f
 13  where not exists (select null
 14                    from service s
 15                    where s.id_supplier = f.id_supplier
 16                      and (   s.planned_value  is null
 17                           or s.executed_value is null
 18                          )
 19                   );

ID_SUPPLIER SUPPLIER_NAME
----------- ---------------
          1 ABC

SQL>

如果设置了所有值:

SQL> with
  2  supplier (id_supplier, supplier_name) as
  3    (select 1, 'ABC' from dual union all
  4     select 2, 'DEF' from dual
  5    ),
  6  service (id_supplier, planned_Value, executed_value) as
  7    (select 1, 100, 100  from dual union all
  8     select 2, 200, 200  from dual union all
  9     select 2, 300, 300  from dual            --> EXECUTED_VALUE is now 300 (was NULL)
 10    )
 11  select f.id_supplier, f.supplier_name
 12  from supplier f
 13  where not exists (select null
 14                    from service s
 15                    where s.id_supplier = f.id_supplier
 16                      and (   s.planned_value  is null
 17                           or s.executed_value is null
 18                          )
 19                   );

ID_SUPPLIER SUPPLIER_NAME
----------- ---------------
          1 ABC
          2 DEF

SQL>
vvppvyoh

vvppvyoh2#

您可以使用分析查询:

SELECT f.supplier_id,
       s.planned_value,
       s.executed_value
FROM   (
  SELECT f.supplier_id,
         s.planned_value,
         s.executed_value,
         COUNT(CASE WHEN s.executed_value IS NULL THEN 1 END)
           OVER (PARTITION BY f.supplier_id) AS has_null
  FROM   supplier f
         INNER JOIN service s
         ON f.supplier_id = s.id_supplier
)
WHERE  has_null = 0
bjp0bcyl

bjp0bcyl3#

您可能不需要执行表联接。我建议从供应商表中选择所有记录,并对每个记录使用exists()检查服务表中是否有EXECUTED_VALUE为空的记录。

select f.supplier_id, f.supplier_name
from supplier f
where not exists (
    select null 
      from service s 
      where f.id_supplier = s.id_supplier 
        and s.executed_value is null
    )

相关问题