oracle 在查询的where子句中使用“case when”

3df52oht  于 2023-11-17  发布在  Oracle
关注(0)|答案(2)|浏览(272)

我创建了一个查询以从表中获取输出。如果参数为“FULL RUN”,我希望查询不具有任何条件并完全运行。如果属性参数为“Changed Attributes”,则我希望查询仅在last_update_date大于另一个表的处理日期时才给予结果。如何使用这种情况以及何时使用?
查询看起来像这样-

select * from 
(SELECT DISTINCT fs.set_code,
                GRADE_LAD.NAME
                GRADE_LADDER,
                To_char(GRADE_LAD.effective_start_date, 'yyyy/mm/dd')
                GRADE_LAD_START_DATE,
                GRADE_LAD.active_status,
                GRADE_LAD.attribute1
                SAL_PLAN_DESCR,
                GRADE_LAD.attribute2
                SHORT_DESCR,
                Max(GRADE_LAD.last_update_date)
                  OVER(
                    partition BY GRADE_LAD.grade_ladder_id)
                max_update_date
FROM   fnd_setid_assignments fsa,
       fnd_setid_sets_vl fs,
       per_grade_ladders_f_vl GRADE_LAD,
       fun_all_business_units_v bu
WHERE  1 = 1
       AND fsa.determinant_value = bu.bu_id
       AND fsa.determinant_type = 'BU'
       AND fsa.set_id = fs.set_id
       AND FS.set_id = grade_set_id
       AND fsa.reference_group_name LIKE 'PER_GRADES'
       AND :EffectiveDate BETWEEN GRADE_LAD.effective_start_date AND
                                  GRADE_LAD.effective_end_date
       --AND GRADE_LAD.NAME LIKE '%Test%' 
    )
    where case when (:attribute = 'Full Run') 
    then 1
            when (:attribute='Changed Attribute') 
            then 
            1
            else 0
       end = 1

字符串
现在我在“then”子句中传递1,但我希望完全运行then子句应该有1=1,但在更改属性的情况下,应该运行以下查询

max_update_date >=
                  Nvl(

(SELECT Max(processstart)
FROM   fusion.ess_request_history erh,
       fusion.ess_request_property erp
WHERE  erh.requestid = erp.requestid
       AND state = 12
       AND erp.NAME = 'reportID'
       AND erp.value LIKE '/Custom/Integrations/Extract.xdo'
),max_update_date)

wb1gzix0

wb1gzix01#

你可以在when的情况下添加额外的条件:

where
  case
    when :attribute = 'Full Run'
    then 1
    when :attribute='Changed Attribute'
    and max_update_date >=
      Nvl(
        (SELECT Max(processstart)
        FROM   fusion.ess_request_history erh,
               fusion.ess_request_property erp
        WHERE  erh.requestid = erp.requestid
        AND state = 12
        AND erp.NAME = 'reportID'
        AND erp.value LIKE '/Custom/Integrations/Extract.xdo'
        ),max_update_date)
    then 1
    else 0
  end = 1

字符串
但是,忘记case表达式,只使用布尔逻辑会更简单:

where :attribute = 'Full Run'
or (
  :attribute='Changed Attribute'
  and max_update_date >=
    Nvl(
      (SELECT Max(processstart)
      FROM   fusion.ess_request_history erh,
             fusion.ess_request_property erp
      WHERE  erh.requestid = erp.requestid
      AND state = 12
      AND erp.NAME = 'reportID'
      AND erp.value LIKE '/Custom/Integrations/Extract.xdo'
      ),max_update_date)
)

pzfprimi

pzfprimi2#

请给予旧的带有表列表的sintax,使用Join ON()sintax。基本上,如果你想在:attribute = 'Full Run'时显示所有行,并且你想在:attribute不同的情况下过滤行,那么你可以在:attribute = 'Full Run'时将Where子句中的独特列与其自身进行比较(始终为true),并在case表达式中将真实的条件放在else或另一个中。
类似下面的东西:

WITH        --  S a m p l e    D a t a :
    tbl_1 AS
        (   Select 1 "ID", 'A' "A_LETTER", 101 "A_NUMBER" From Dual Union ALl
            Select 2 "ID", 'B' "A_LETTER", 102 "A_NUMBER" From Dual Union ALl
            Select 3 "ID", 'C' "A_LETTER", 103 "A_NUMBER" From Dual Union ALl
            Select 4 "ID", 'D' "A_LETTER", 104 "A_NUMBER" From Dual Union ALl
            Select 5 "ID", 'E' "A_LETTER", 105 "A_NUMBER" From Dual
        ),
    tbl_2 AS
        (   Select 1 "ID", 'A104' "COMB" From Dual Union ALl
            Select 2 "ID", 'X102' "COMB" From Dual Union ALl
            Select 3 "ID", 'C103' "COMB" From Dual Union ALl
            Select 4 "ID", 'D104' "COMB" From Dual Union ALl
            Select 5 "ID", 'Y105' "COMB" From Dual 
        )
--  M a i n   S Q L
Select      t1.ID, t1.A_LETTER, t1.A_NUMBER, t2.COMB
From        tbl_1 t1
Inner Join  tbl_2 t2 ON(t1.ID = t2.ID)
Where       t2.COMB = Case When :attribute = 'Full Run' Then t2.COMB Else t1.A_LETTER || t1.A_NUMBER End

/*  attribute = 'Full Run'
        ID A_LETTER   A_NUMBER COMB
---------- -------- ---------- ----
         1 A               101 A104
         2 B               102 X102
         3 C               103 C103
         4 D               104 D104
         5 E               105 Y105   */
/*  other attribute
        ID A_LETTER   A_NUMBER COMB
---------- -------- ---------- ----
         3 C               103 C103
         4 D               104 D104    */

字符串

相关问题