user2937985
user2937985

Reputation: 1

Using AND and OR in a where clause

I'm having a hard time getting the correct results with this where clause. I know it is a paraenthesis issue. Any suggestions?

WHERE a.EVENT_DATE Between &trend_dt  And &enddate  
AND a.INDICATOR Not In ('INQUIRY (NOS)','PCC') 
And a.INDICATOR2 Not In ('CAT-NA','INQUIRY (NOS)') 
And d.ACTIVE In ('C','F') 
And d.ARCHIVE_STATUS in ('N','Y') 
Or (d.ARCHIVE_STATUS Is Null)

Thanks!

The problem with the results is that the Indicator 2 field is sometimes blank and it is only returning records where there is something in the Indictator 2 field.

This is the query:

PROC SQL;
CREATE TABLE COMPLAINT_DATA_0 as
SELECT 
a.EVENT_DATE,
a.INDICATOR,
a.INDICATOR2,
a.COMMITTEE_ACTION AS REVIEW_TYPE,
a.LEVEL_OF_EXCEPTION AS METHOD_RECD,
a.REFER_TO AS PLAN_NTWK,
b.DESCRIPTION AS SubCategory, 
d.CROSSREF2 AS ConProvID, 
d.LAST_NAME AS Provider, 
a.MED_REC_NO AS MbrID, 
a.CONFIRMED_BY AS IndProvID, 
a.CODE_15 AS Severity, 
MAX(c.INQUIRY_BODY) AS MaxProcStep, 
e.Notes,
e.Notes2,
FROM VIPDBA.QA_PEER AS a LEFT JOIN VIPDBA.SYS_LOOKUPS AS b 
ON a.INDICATOR2=b.CODE 
LEFT JOIN VIPDBA.QA_PEER_INQUIRY AS c 
     ON a.LOOKUP_KEY=c.LOOKUP_KEY AND 
a.MED_REC_NO=c.MED_REC_NO 
LEFT JOIN VIPDBA.MASTER_CRED AS d 
    ON a.LOOKUP_KEY=d.LOOKUP_KEY 
LEFT JOIN Complaint_Notes AS e 
    ON a.LOOKUP_KEY=e.LOOKUP_KEY AND 
a.MED_REC_NO=e.MbrID
WHERE a.EVENT_DATE Between &trend_dt  And &enddate  
    AND a.INDICATOR Not In ('INQUIRY (NOS)','PCC') 
    And a.INDICATOR2 Not In ('CAT-NA','INQUIRY (NOS)') 
    And d.ACTIVE In ('C','F') 
    And 
        ( d.ARCHIVE_STATUS IN ('N','Y') 
        OR d.ARCHIVE_STATUS is null)
GROUP a.EVENT_DATE, 
a.INDICATOR,
a.INDICATOR2,
b.DESCRIPTION, 
d.CROSSREF2, 
d.LAST_NAME, 
a.MED_REC_NO, 
a.CONFIRMED_BY, 
a.CODE_15, 
e.Notes,
e.Notes2,
d.archive_status,
d.active
ORDER BY 
a.EVENT_DATE, 
a.INDICATOR2, 
b.DESCRIPTION, 
d.CROSSREF2, 
d.LAST_NAME

; run;

Upvotes: 0

Views: 112

Answers (4)

Pavel F
Pavel F

Reputation: 21

Or you can even avoid using OR :)

WHERE a.EVENT_DATE Between &trend_dt  And &enddate  
AND a.INDICATOR Not In ('INQUIRY (NOS)','PCC') 
And a.INDICATOR2 Not In ('CAT-NA','INQUIRY (NOS)') 
And d.ACTIVE In ('C','F') 
And COALESCE(d.ARCHIVE_STATUS,'-1') in ('N','Y','-1')

Upvotes: 2

wildplasser
wildplasser

Reputation: 44230

A trick to avoid the checking-for NULL (and thus avoiding the need for an OR ...) is to transform the NULL to a comparable value, like in:

WHERE a.EVENT_DATE BETWEEN &trend_dt AND &enddate  
AND a.INDICATOR NOT IN ('INQUIRY (NOS)','PCC') 
AND a.INDICATOR2 NOT IN ('CAT-NA','INQUIRY (NOS)') 
AND d.ACTIVE IN ('C','F') 
AND COALESCE(d.ARCHIVE_STATUS, 'N') IN ('N','Y') 
   ;

Upvotes: 2

Mike Monge
Mike Monge

Reputation: 66

Is it that you need to see results where d.ARCHIVE_STATUS in ('N','Y') Or (d.ARCHIVE_STATUS Is Null) if so, you will probably just need to change it to look like this

WHERE a.EVENT_DATE Between &trend_dt  And &enddate  
AND a.INDICATOR Not In ('INQUIRY (NOS)','PCC') 
And a.INDICATOR2 Not In ('CAT-NA','INQUIRY (NOS)') 
And d.ACTIVE In ('C','F') 
And (
      d.ARCHIVE_STATUS in ('N','Y') 
      Or 
      d.ARCHIVE_STATUS Is Null
    )

This way it will only evaluate d.ARCHIVE_STATUS in ('N','Y') against d.ARCHIVE_STATUS Is Null

Upvotes: 0

David Fleeman
David Fleeman

Reputation: 2638

I think you are looking for this:

WHERE a.EVENT_DATE Between &trend_dt  And &enddate  
AND a.INDICATOR Not In ('INQUIRY (NOS)','PCC') 
And a.INDICATOR2 Not In ('CAT-NA','INQUIRY (NOS)') 
And d.ACTIVE In ('C','F') 
And ( d.ARCHIVE_STATUS in ('N','Y') Or d.ARCHIVE_STATUS Is Null )

Use parenthesis to group logic together.

Upvotes: 2

Related Questions