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

私はあなたがこれを探していると思います:

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 に答える
2

NULL のチェックを回避する (したがって の必要性を回避する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 に答える
0

d.ARCHIVE_STATUS in ('N','Y') Or (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
    )

このように、d.ARCHIVE_STATUS Is Null に対して ('N','Y') の d.ARCHIVE_STATUS のみを評価します。

于 2013-10-30T15:52:28.413 に答える