2016-10-11 83 views
0
SELECT * FROM [dbo].[_5200_Sanoma] 
WHERE right(left([VARIABLE1)],4),2) = 'RI' 
and (
     ([Year] = '2014' and [Period] in('10','11','12')) 
     or [Year] = '2015') 
     or (
       [Year] = '2016' 
       and [Period] in('01','02','03','04','05','06','07','08')--,'09','10','11','12') 
      ) 
and ([VARIABLE2] IN(
'String1', 
'String2', 
'String3', 
'String4', 
'String5', 
'String6', 
)) 

我不得不改變一些事情要更普遍,但由於某種原因,第一where子句中,right(left([VARIABLE1)],4),2) = 'RI'不工作,因爲我得到返回結果,其中,它是其他兩個字符串。SQL WHERE子句不返回正確的結果

之前,我添加了最後這個查詢工作where子句條件

and ([VARIABLE2] IN(
    'String1', 
    'String2', 
    'String3', 
    'String4', 
    'String5', 
    'String6', 
    )) 

所以,現在,我補充說,它不工作。有任何想法嗎?

回答

0

儘量拆分在不同線路的語句一些括號問題;你大概是這樣做的:

SELECT * FROM [dbo].[_5200_Sanoma] 
WHERE right(left([VARIABLE1)],4),2) = 'RI' AND 
(
    ([Year] = '2014' and [Period] in('10','11','12')) OR 
    ([Year] = '2015') OR 
    ([Year] = '2016' and [Period] in('01','02','03','04','05','06','07','08')) 
) 
and ([VARIABLE2] IN ('String1','String2','String3','String4','String5','String6')) 
+0

謝謝,這個工作! – SanomaJean

0

猜你只是有你或條款(其中之一是不包含在AND部分後WHERE right(left([VARIABLE1)],4),2) = 'RI'

嘗試

and (
      ([Year] = '2014' and [Period] in('10','11','12')) or 
      [Year] = '2015' or 
      ([Year] = '2016' and [Period] in('01','02','03','04','05','06','07','08')--,'09','10','11','12') 
     )