Search code examples
oracle-databasefilterwhere-clause

filters in where statement not working for oracle query


In my oracle query:

Select
d.LVL_NAME
,d.SCHEDULE_DESC
,d.SCHEDULE_TYPE
,s.DUE_DTTM
,s.WARNING_DTTM
,s.DELINQUENT_DTTM
,d.EST_WORK_DURATION
,d.EST_WORK_DURATION_UNIT
,l.ENTITY_GRP
from smsdw.TW_SCHEDULE_DEF d
RIGHT OUTER JOIN
smsdw.TW_ENTITY_SCHED s on
d.LVL_NAME = s.ENTITY
LEFT OUTER JOIN smsdw.TW_ENTITY_GRP_LST l on
s.ENTITY = l.ENTITY

WHERE l.ENTITY_GRP NOT LIKE '%Wet%' 
  AND l.ENTITY_GRP LIKE 'Implant%' or l.ENTITY_GRP LIKE 'Thermal%' 
  and s.DUE_DTTM between sysdate and sysdate +30 ;

I cannot figure out how to get the "NOT LIKE WET" and the "sysdate" filter to work in my WHERE statement. Any ides? I have inserted images of my results. l.ENTITY_GRP NOT LIKE '%Wet%' s.DUE_DTTM between sysdate and sysdate +30

I have tried many different ways that I have found from googling, but I still get the same results listed in my problem.


Solution

  • Add ()'s around your OR. assuming all ANDS are meant to be applied to whole set.

    WHERE l.ENTITY_GRP NOT LIKE '%Wet%' 
      AND (l.ENTITY_GRP LIKE 'Implant%' or l.ENTITY_GRP LIKE 'Thermal%')
      AND s.DUE_DTTM between sysdate and sysdate +30 ;
    

    What's happening is the OR says bring back any group like thermal regardless if it's not like wet and not within the due_dttm date range.