-1

以下のIF文をSQLで書きたいと思います。where句のほとんどは構築されていますが、私が苦労しているのはステートメントの周りの条件です:

if @StuYear = 11 then

AND (@TeachingGroup = 'Select All')
AND ([DataCollection] = @DataCollection)
AND ([Name] = @SubjectName) 
AND (@Subgroup='Select All') 
AND '' = CASE @subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END 
AND Result Not Like '[ABU]%' AND Result <> ''

else if @StuYear = 10 then 

AND @TeachingGroup Not Like 'Select All' 
AND ([DataCollection] = @DataCollection) 
AND ([Name] = @SubjectName) 
AND ([TeachingGroup] = @TeachingGroup)  
AND @Subgroup='Select All' 
AND '' = CASE @subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END 
AND Result Not Like '[ABC]%' AND Result <> ''

end if
4

2 に答える 2

1

とを置き換えifて、他の条件を括弧で囲みます。whereelse ifor

...
where (@StuYear = 11 
AND (@TeachingGroup = 'Select All')
AND ([DataCollection] = @DataCollection)
AND ([Name] = @SubjectName) 
AND (@Subgroup='Select All') 
AND '' = CASE @subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END 
AND Result Not Like  'A*' 
AND Result Not Like 'A' 
AND Result Not Like 'B' 
AND Result Not Like 'U' 
AND Result Not Like '' )

OR (@StuYear = 10 
AND @TeachingGroup Not Like 'Select All' 
AND ([DataCollection] = @DataCollection) 
AND ([Name] = @SubjectName) 
AND ([TeachingGroup] = @TeachingGroup)  
AND @Subgroup='Select All' 
AND '' = CASE @subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END 
AND Result  Not Like  'A*' 
AND Result  Not Like  'A' 
AND Result Not Like 'B' 
AND Result  Not Like  'C' 
AND Result  Not Like '')
于 2013-05-13T21:01:27.930 に答える
0

ティムの答えは、私を正しい方向に導くのに役立ちました。ただし、最終的には、年条件を where 句の周りにラップするのではなく適用することで、問題を解決しました。

where 
    (@StuYear = [stuyear]
    AND @TeachingGroup = 'Select All'
    AND [DataCollection] = @DataCollection
    AND [Name] = @SubjectName
    AND @Subgroup='Select All'
    AND '' = CASE @subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
    AND ((Result Not Like '[ABU]%' and result not like '' and stuyear='11')
            or(Result Not Like '[ABC]%' and result not like '' and stuyear='10')
            or(Result Not Like '[ABCD]%' and stuyear='9'))
    AND Result <> '')
OR 
    (@StuYear = [stuyear]
        AND @TeachingGroup Not Like 'Select All'
        AND [DataCollection] = @DataCollection
        AND [Name] = @SubjectName
        AND [TeachingGroup] = @TeachingGroup
        AND @Subgroup='Select All'
        AND '' = CASE @subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
        AND ((Result Not Like '[ABU]%' and result not like '' and stuyear='11')
                or(Result Not Like '[ABC]%' and result not like '' and stuyear='10')
                or(Result Not Like '[ABCD]%' and stuyear='9'))
        AND Result <> '') 
于 2013-05-15T13:26:21.690 に答える