0

我很难用这个 where 子句得到正确的结果。我知道这是一个括号问题。有什么建议么?

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)

谢谢!

结果的问题在于,Indicator 2 字段有时是空白的,它只返回 Indictator 2 字段中有内容的记录。

这是查询:

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

; 跑;

4

4 回答 4

2

避免检查 NULL (从而避免需要 an OR ...)的技巧是将 NULL 转换为可比较的值,例如:

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') 
   ;
于 2013-10-30T15:53:17.980 回答
2

或者你甚至可以避免使用 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')
于 2013-10-30T15:53:38.423 回答
2

我想你正在寻找这个:

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 )

使用括号将逻辑组合在一起。

于 2013-10-30T15:47:56.030 回答
0

您是否需要查看 d.ARCHIVE_STATUS in ('N','Y') 或 (d.ARCHIVE_STATUS Is Null) 的结果,如果是这样,您可能只需将其更改为如下所示

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
    )

这样它只会评估 ('N','Y') 中的 d.ARCHIVE_STATUS 与 d.ARCHIVE_STATUS Is Null

于 2013-10-30T15:52:28.413 回答