Home > Enterprise >  how to use a case statement in a sql where condition
how to use a case statement in a sql where condition

Time:01-06

i have a sql in the below format:

select distinct
      ora_hash( ah.target_name 
                   || to_char( start_timestamp, 'DD-MON-YY HH24:MI:SS' ))
         || ','
         || 'Critical'
         || ','
         || host_name
         || ','
         || ah.target_name
         || ','
         || 'Instance unexpectedly shutdown at '
         || to_char( start_timestamp, 'DD-MON-YY HH24:MI:SS' )
   from 
      sysman_ro.mgmt$availability_history ah
         join sysman_ro.mgmt$target_members tm 
            on ah.target_name = tm.member_target_name
         join sysman_ro.mgmt$target mt 
            on ah.target_name = mt.target_name
            left outer join sysman_ro.mgmt$blackout_history bh  
               on mt.target_name = bh.target_name
   where 
          tm.aggregate_target_name like 'PROD_DB'
      and ah.availability_status_code = 0
      and ah.start_timestamp > sysdate -0.2
      and ah.start_timestamp > bh.start_time
      and ah.target_type = 'oracle_database'

Now the issue is that bh.start_time does not return anything for few targets. So here i wanted to introduce a case statement such that if bh.start_time has a value(like 08-NOV-22) then the condition 'ah.start_timestamp > bh.start_time' should be taken into account and if there is no value returned for bh.start_time then the condition 'ah.start_timestamp > bh.start_time' should be skipped.

is this possible in the 'where'condition. Thanks.

CodePudding user response:

That's not a CASE statement, it's just an OR condition. For example:

AND (bh.start_time IS NULL OR ah.start_timestamp > bh.start_time)

So the overall condition is satisfied if start_time is NULL or if start_timestamp > start_time.

CodePudding user response:

First - CASE is an expression (not a statement). It works sequetialy and the first WHEN condition that is True will return THEN value and EXIT the CASE. That means you should order those when conditions in a way that it will give the result you want.
A small sample data (we don't know your data):

WITH
    ah AS
        (
            Select 1 "AH_ID", To_Date('08.11.2022 13:00:00', 'dd.mm.yyyy hh24:mi:ss') "AH_DATE" From Dual Union All
            Select 2 "AH_ID", To_Date('08.11.2022 16:00:00', 'dd.mm.yyyy hh24:mi:ss') "AH_DATE" From Dual Union All
            Select 3 "AH_ID", To_Date('08.11.2022 19:00:00', 'dd.mm.yyyy hh24:mi:ss') "AH_DATE" From Dual Union All
            Select 4 "AH_ID", To_Date('08.11.2022 22:00:00', 'dd.mm.yyyy hh24:mi:ss') "AH_DATE" From Dual Union All
            Select 5 "AH_ID", To_Date('08.11.2022 23:00:00', 'dd.mm.yyyy hh24:mi:ss') "AH_DATE" From Dual 
        ),
    bh AS
        (
            Select 1 "BH_ID", 1 "AH_ID", To_Date('08.11.2022 07:00:00', 'dd.mm.yyyy hh24:mi:ss') "BH_DATE" From Dual Union All
            Select 2 "BH_ID", 1 "AH_ID", To_Date('08.11.2022 09:00:00', 'dd.mm.yyyy hh24:mi:ss') "BH_DATE" From Dual Union All
            Select 3 "BH_ID", 3 "AH_ID", To_Date('08.11.2022 19:00:00', 'dd.mm.yyyy hh24:mi:ss') "BH_DATE" From Dual Union All
            Select 4 "BH_ID", 3 "AH_ID", To_Date('08.11.2022 20:00:00', 'dd.mm.yyyy hh24:mi:ss') "BH_DATE" From Dual Union All
            Select 5 "BH_ID", 5 "AH_ID", To_Date('08.11.2022 21:00:00', 'dd.mm.yyyy hh24:mi:ss') "BH_DATE" From Dual 
        )

If we left join the tables and if it was like
SYSDATE = To_Date('09.11.2022 01:00:00', 'dd.mm.yyyy hh24:mi:ss')
I put in selection SYSDATE_LIMIT column just to be able to visualy compare the values that will be filtered later:

Select
    ah.AH_ID, 
    To_Char(ah.AH_DATE, 'dd.mm.yyyy hh24:mi:ss') "AH_DATE",
  To_Char(To_Date('09.11.2022 01:00:00', 'dd.mm.yyyy hh24:mi:ss') - 0.2, 'dd.mm.yyyy hh24:mi:ss') "SYSDATE_LIMIT",
    bh.BH_ID, 
    To_Char(bh.BH_DATE, 'dd.mm.yyyy hh24:mi:ss') "BH_DATE"
From
    ah
Left Join
    bh ON(bh.AH_ID = ah.AH_ID)

Result is:

AH_ID AH_DATE SYSDATE_LIMIT BH_ID BH_DATE
1 08.11.2022 13:00:00 08.11.2022 20:12:00 1 08.11.2022 07:00:00
1 08.11.2022 13:00:00 08.11.2022 20:12:00 2 08.11.2022 09:00:00
2 08.11.2022 16:00:00 08.11.2022 20:12:00
3 08.11.2022 19:00:00 08.11.2022 20:12:00 3 08.11.2022 19:00:00
3 08.11.2022 19:00:00 08.11.2022 20:12:00 4 08.11.2022 20:00:00
4 08.11.2022 22:00:00 08.11.2022 20:12:00
5 08.11.2022 23:00:00 08.11.2022 20:12:00 5 08.11.2022 21:00:00

One of the ways to do the filtering in WHERE clause using CASE expression could be like here (comments in code)

... ... ...
Where
    CASE 
        WHEN bh.BH_ID Is Null THEN 0            -- there is no matching row in bh table - excluded
        WHEN ah.AH_DATE <= bh.BH_DATE THEN 0        -- AH_DATE is not greater than BH_DATE - excluded
        WHEN ah.AH_DATE <= To_Date('09.11.2022 01:00:00', 'dd.mm.yyyy hh24:mi:ss') - 0.2 THEN 0  -- AH_DATE is not greater than SYSDATE - 0.2 - excluded
    ELSE 1      -- there is a row in bh table where AH_DATE is greater than BH_DATE and AH_DATE is greater than SYSDATE - 0.2   -  row returned
    END = 1

Result:

AH_ID AH_DATE SYSDATE_LIMIT BH_ID BH_DATE
5 08.11.2022 23:00:00 08.11.2022 20:12:00 5 08.11.2022 21:00:00
  • Related