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

问题描述 投票:0回答:1

我创建了一个查询来从表中获取输出。如果参数是“FULL RUN”,我希望查询没有任何条件并完全运行。如果属性参数是“更改的属性”,那么我希望查询仅在 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)
sql oracle oracle-sqldeveloper
1个回答
0
投票

您可以在案例中添加额外的条件

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)
)
© www.soinside.com 2019 - 2024. All rights reserved.