oracle 在结果查询中重用When条件

mv1qrgav  于 2023-02-18  发布在  Oracle
关注(0)|答案(4)|浏览(160)

我做了这个查询,它工作,但有可能做一些更有效和可读?我只是想有一个查询的结果(我不想使用pl/sql).最好的是将when条件存储在一个变量中,但我不知道这是否可能.

SELECT 
CASE WHEN (SELECT min(handling_unit_id) FROM ifsapp.handling_unit_shipment where  
        shipment_id = '1371'
        AND ifsapp.HANDLING_UNIT_TYPE_API.Get_Handling_Unit_Category_Id(handling_unit_type_id) like 'SST_N1'
        and structure_level = 1)  is not null
 THEN (SELECT min(handling_unit_id) FROM ifsapp.handling_unit_shipment where  
        shipment_id = '1371'
        AND ifsapp.HANDLING_UNIT_TYPE_API.Get_Handling_Unit_Category_Id(handling_unit_type_id) like 'SST_N1'
        and structure_level = 1)
 ELSE (SELECT min(handling_unit_id) FROM ifsapp.handling_unit_shipment where  
        shipment_id = '1371'
        AND ifsapp.HANDLING_UNIT_TYPE_API.Get_Handling_Unit_Category_Id(handling_unit_type_id) like 'SST_N1'
        and structure_level > 1)
END HUI FROM dual ;
j2cgzkjk

j2cgzkjk1#

您可以使用以下查询(如果您需要当前示例中的单个值):

select handling_unit_id
from ifsapp.handling_unit_shipment
where shipment_id = '1371'
  and ifsapp.HANDLING_UNIT_TYPE_API.Get_Handling_Unit_Category_Id(
    handling_unit_type_id
  ) like 'SST_N1'
  and structure_level >= 1
order by structure_level asc, handling_unit_id asc
fetch first row only

它将做什么:
1.它按structure_level对数据进行升序排序,因此如果structure_level >= 1存在,则返回structure_level = 1作为第一行,如果不存在,则返回structure_level > 1
1.如果每个结构值有许多handling_unit_id,那么它将把最少的handling_unit_id放在第一行,这相当于min

wmvff8tz

wmvff8tz2#

您可以使用coalesce函数使其更紧凑一些

SELECT coalesce(
  (SELECT min(handling_unit_id) FROM ifsapp.handling_unit_shipment where  
    shipment_id = '1371'
    AND ifsapp.HANDLING_UNIT_TYPE_API.Get_Handling_Unit_Category_Id(handling_unit_type_id) like 'SST_N1'
    and structure_level = 1),
  (SELECT min(handling_unit_id) FROM ifsapp.handling_unit_shipment where  
    shipment_id = '1371'
    AND ifsapp.HANDLING_UNIT_TYPE_API.Get_Handling_Unit_Category_Id(handling_unit_type_id) like 'SST_N1'
    and structure_level > 1)
  )
FROM dual ;

coalesce函数基本上返回其输入参数中的第一个非空值

mwkjh3gx

mwkjh3gx3#

您可以使用查询因子分解或common-table-expression。

with tb as (
    SELECT min(handling_unit_id) min_hu_id
    FROM ifsapp.handling_unit_shipment 
    where shipment_id = '1371' 
      AND ifsapp.HANDLING_UNIT_TYPE_API.Get_Handling_Unit_Category_Id(handling_unit_type_id) like 'SST_N1'
      and structure_level = 1)
)
select coalesce(min_hu_id
  ,(SELECT min(handling_unit_id) 
    FROM ifsapp.handling_unit_shipment 
    where shipment_id = '1371'
      AND ifsapp.HANDLING_UNIT_TYPE_API.Get_Handling_Unit_Category_Id(handling_unit_type_id) like 'SST_N1'
      and structure_level > 1)
from tb 
;
c86crjj0

c86crjj04#

另一种选择。找出所有三个查询的共同点,并在CTE(WITH子句)中执行一次。然后以各自的方式查询,以查找每种情况下的不同之处。执行CASE/IF-THEN类型逻辑有多种模式。这里有一种方法。它使用一个棘手的SIGN操作,以便将所有〉1的structure_level值组合在一起。

WITH data AS (SELECT SIGN(structure_level-1)+1 structure_level_group,min(handling_unit_id) min_handling_unit_id
                FROM ifsapp.handling_unit_shipment 
               WHERE shipment_id = '1371'
                 AND ifsapp.HANDLING_UNIT_TYPE_API.Get_Handling_Unit_Category_Id(handling_unit_type_id) like 'SST_N1'
               GROUP BY SIGN(structure_level-1)+1)
SELECT NVL(t1.min_handling_unit_id,t2.min_handling_unit_id) hui
  FROM (SELECT MIN(min_handling_unit_id)  min_handling_unit_id
          FROM data
         WHERE structure_level_group = 1) t1,
       (SELECT MIN(min_handling_unit_id) min_handling_unit_id
          FROM data
         WHERE structure_level_group = 2) t2

相关问题