Unrelated result in the output?

我们两清 提交于 2019-12-11 13:22:26

问题


this is my query:

SELECT TOP 10000 [Service_order]
      ,[COMPANY]
      ,[PENDING_DAYS]
      ,[SERVICE_TYPE]
      ,[SERVICE_TYPE_TXT]
      ,[STATUS]
      ,[STATUS_TEXT]
      ,[REASON]
      ,[REASON_TEXT]
      ,[ASC code]
      ,[ASC name]
      ,[MODEL]
      ,[INOUTWTY]
      ,[Part_code1]
      ,[PS1]
      ,[confirmation_No1]
      ,[Part_code2]
      ,[PS2]
      ,[SO_NO2]
      ,[Part_code3]
      ,[PS3]
      ,[SO_NO3]
      ,[Part_code4]
      ,[PS4]
      ,[SO_NO4]
      ,[Part_code5]
      ,[PS5]
      ,[SO_NO5]
      ,[Engineer name]
  FROM ['NewLP']
where REASON_TEXT = 'Parts Not available (ASC)'
or  REASON_TEXT = 'Parts In Transit (Samsung)'
or REASON_TEXT = 'Parts Back Ordered (Samsung)'
  and PS1 = 'U' 
  and PS2 = 'U' or PS2 = ''
  and PS3 = 'U' or PS3 = ''
  and PS4 = 'U' or PS4 = ''
  and PS5 = 'U' or PS5 = ''

I don't know which part is wrong exactly but the reason_text should be those in the condition and first ps should be 'U' and the rest should be 'U' or empty, but when I run the query the result is not what it should be. here is the results:

And when I use one of the reason_text in the condition only first two PS's show correctly, the rest(PS3,PS4,PS5) is not following the condition?

here is the result when I use one of the reason_Text, I only selected the columns that are important.

I would appreciate any help.


回答1:


You need to put your PS* and REASON TEXT where using OR conditions into brackets like this:

where (
      REASON_TEXT = 'Parts Not available (ASC)'
or    REASON_TEXT = 'Parts In Transit (Samsung)'
or    REASON_TEXT = 'Parts Back Ordered (Samsung)'
      )
and   PS1 = 'U' 
and   ( PS2 = 'U' or PS2 = '' )
and   ( PS3 = 'U' or PS3 = '' )
and   ( PS4 = 'U' or PS4 = '' )
and   ( PS5 = 'U' or PS5 = '' )

Remember that AND operator has precedence over OR and when combining these conditions, it is important to use parentheses so that the database knows in which order to evaluate each condition.

Full query

SELECT TOP 10000 [Service_order]
      ,[COMPANY]
      ,[PENDING_DAYS]
      ,[SERVICE_TYPE]
      ,[SERVICE_TYPE_TXT]
      ,[STATUS]
      ,[STATUS_TEXT]
      ,[REASON]
      ,[REASON_TEXT]
      ,[ASC code]
      ,[ASC name]
      ,[MODEL]
      ,[INOUTWTY]
      ,[Part_code1]
      ,[PS1]
      ,[confirmation_No1]
      ,[Part_code2]
      ,[PS2]
      ,[SO_NO2]
      ,[Part_code3]
      ,[PS3]
      ,[SO_NO3]
      ,[Part_code4]
      ,[PS4]
      ,[SO_NO4]
      ,[Part_code5]
      ,[PS5]
      ,[SO_NO5]
      ,[Engineer name]
  FROM ['NewLP']
  where (
        REASON_TEXT = 'Parts Not available (ASC)'
  or    REASON_TEXT = 'Parts In Transit (Samsung)'
  or    REASON_TEXT = 'Parts Back Ordered (Samsung)'
        )
  and   PS1 = 'U' 
  and   ( PS2 = 'U' or PS2 = '' )
  and   ( PS3 = 'U' or PS3 = '' )
  and   ( PS4 = 'U' or PS4 = '' )
  and   ( PS5 = 'U' or PS5 = '' )



回答2:


execution order in where clause is not correct.To control do like below

where (REASON_TEXT = 'Parts Not available (ASC)'
or  REASON_TEXT = 'Parts In Transit (Samsung)'
or REASON_TEXT = 'Parts Back Ordered (Samsung)')
  and PS1 = 'U' 
  and (PS2 = 'U' or PS2 = '')
  and (PS3 = 'U' or PS3 = '')
  and (PS4 = 'U' or PS4 = '')
  and (PS5 = 'U' or PS5 = '')



回答3:


Try adding parenthesis like this:

  ...
  and PS1 = 'U' 
  and (PS2 = 'U' or PS2 = '')
  and (PS3 = 'U' or PS3 = '')
  and (PS4 = 'U' or PS4 = '')
  and (PS5 = 'U' or PS5 = '')



回答4:


You have to use brackets. AND is evaluated before OR in case of no brackets. PS1 = 'U' and PS2 = 'U' or PS2 = '' and PS3 = 'U' has the same meaning as (PS1 = 'U' and PS2 = 'U') or (PS2 = '' and PS3 = 'U') for example.




回答5:


The problem is AND has higher precedence than OR operator. So you need proper parenthesis to control the execution order.

your Where clause can be simplified like this

where REASON_TEXT in ('Parts Not available (ASC)',
                      'Parts In Transit (Samsung)',
                      'Parts Back Ordered (Samsung)')
  and PS1 = 'U' 
  and PS2 in ('U','')
  and PS3 in ('U','')
  and PS4 in ('U','')
  and PS5 in ('U','')


来源:https://stackoverflow.com/questions/39191687/unrelated-result-in-the-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!