多个和陈述

时间:2017-05-25 15:23:18

标签: sql case

我正在尝试确定两年来记录何时具有AtAboveBelowGradeLevel = 1。换句话说,我将如何处理下面的案例陈述?

SELECT AcademicYear
    ,SchoolName
    ,Subject
    ,LastName
    ,firstname
    ,StudentBKID
    ,AtAboveBelowGradeLevelCount
    ,CASE 
        WHEN AtAboveBelowGradeLevelCount = 1
            AND AcademicYear = '2015-2016'
            AND AcademicYear = '2016-2017'
            AND AtAboveBelowGradeLevelCount = 1
            THEN 'TRUE'
        ELSE 'NO'
        END
FROM StudentAssessmentMart.dbo.vwMAPAssessmentInformation
WHERE AcademicYear IN (
        '2015-2016'
        ,'2016-2017'
        )
    AND SchoolName LIKE 'alliance%'
    AND subject IN ('math')
    AND StudentBKID IN (
        '473106'
        ,'420219'
        )
    AND CalendarPeriodName = 'spring'
GROUP BY AcademicYear
    ,SchoolName
    ,Subject
    ,LastName
    ,firstname
    ,StudentBKID
    ,AtAboveBelowGradeLevelCount
ORDER BY StudentBKID
    ,AcademicYear

1 个答案:

答案 0 :(得分:0)

这是简化版

SELECT 
     StudentBKID
    ,CASE 
        WHEN COUNT(CASE WHEN AtAboveBelowGradeLevelCount  = 1 THEN 1 END) = 2 
        THEN 'TRUE'
        ELSE 'NO'
     END
FROM StudentAssessmentMart.dbo.vwMAPAssessmentInformation
WHERE AcademicYear IN ('2015-2016','2016-2017')
  AND SchoolName LIKE 'alliance%'
  AND subject IN ('math')
  AND StudentBKID IN ('473106','420219')
  AND CalendarPeriodName = 'spring'      
GROUP BY StudentBKID

年份已经在WHERE上过滤,因此您只需要进行条件计数,因此请查看其中有多少是AboveBelowGrade