Search code examples
sqlsql-serverwhere-clause

SQL SELECT and WHERE


I Want to select transactions under PL Category 52105 and transaction codes 9007 & 9803, but the results are are picking other transaction codes other than those 2. Where am I getting it wrong?

FROM [Steward].[dbo].[vwNONFUNDED_RECOMP_04] 
   
WHERE PL_CATEGORY_CATEG_STMT IN ('52105') OR PL_CATEGORY_CATEG_STMT IS NULL AND TRANSACTION_CODE_STMT IN ('9007', '9803') AND REVERSAL_MARKER_STMT NOT LIKE ('R')  

order by BOOKING_DATE_STMT

enter image description here


Solution

  • Its the order of precedence that yields everything behind the OR statement to be treated as just that.

    Try the following instead, forcing the order by using brackets:

    WHERE (PL_CATEGORY_CATEG_STMT IN ('52105') OR PL_CATEGORY_CATEG_STMT IS NULL) AND TRANSACTION_CODE_STMT IN ('9007', '9803') AND REVERSAL_MARKER_STMT NOT LIKE ('R')
    

    Note the two brackets around the two statements participating in the OR clause.