对Int列进行SQL条件过滤

时间:2018-09-27 15:27:07

标签: sql sql-server tsql

我有一张桌子,正在收集学生的反馈。我将运行一个存储过程,以获取学生的所有答复并处理所有的分页,排序和过滤。我遇到的问题是一个int列,该列将存储在他们的响应中发现多少亵渎的计数。我将使用一个过滤器,该过滤器将具有“是” /“否” /“全部”选项,这些选项将确定您是否要查看带有亵渎性内容的响应,不带或不带两者。我不确定如何在可以处理所有3种情况的where子句中获得case语句。

基本上,我在哪里需要类似的东西。 @profanity将是发送到存储过程的变量,该变量将为true,false或null。

WHERE 
(
  --OTHER FILTERS HERE
)
AND
(
    CASE
        -- if @profanity filter is true then we want all with counter greater than 0
        WHEN @Profanity = '1' THEN R.ProfanitiesFound > '0'
        -- else if @profanty filter is false then we want all with counter equal to 0
        WHEN @Profanity = '0' THEN R.ProfanitiesFound = '0'
        -- if nothing was sent in then just show all
        WHEN (@Profanity IS NULL OR @Profanity = '') THEN R.ProfanitiesFound = R.Profanities
    END
)

并非需要其余的查询,而是在这里:

;WITH CTE_Results
AS
(
    SELECT ROW_NUMBER() OVER (ORDER BY
        CASE WHEN @Column = 'Student' AND @Direction = 'asc' THEN u.LastName END asc,
        CASE WHEN @Column = 'Student' AND @Direction = 'desc' THEN u.LastName END desc,
        CASE WHEN @Column = 'Student' THEN u.FirstName END asc,
        CASE WHEN @Column = 'Classroom' AND @Direction = 'asc' THEN c.Name END asc,
        CASE WHEN @Column = 'Classroom' AND @Direction = 'desc' THEN c.Name END desc,
        CASE WHEN @Column = 'Classroom' THEN u.LastName END asc,
        CASE WHEN @Column = 'Lesson' AND @Direction = 'asc' THEN L.Name END asc,
        CASE WHEN @Column = 'Lesson' AND @Direction = 'desc' THEN L.Name END desc,
        CASE WHEN @Column = 'Lesson' THEN u.LastName END asc,
        CASE WHEN @Column = 'Page' AND @Direction = 'asc' THEN P.PageNumber END asc,
        CASE WHEN @Column = 'Page' AND @Direction = 'desc' THEN P.PageNumber END desc,
        CASE WHEN @Column = 'Page' THEN L.Name END asc,
        CASE WHEN @Column = 'QuestionType' AND @Direction = 'asc' THEN QT.Name END asc,
        CASE WHEN @Column = 'QuestionType' AND @Direction = 'desc' THEN QT.Name END desc,
        CASE WHEN @Column = 'QuestionType' THEN u.LastName END asc,
        CASE WHEN @Column = 'Attempt' AND @Direction = 'asc' THEN R.Attempt END asc,
        CASE WHEN @Column = 'Attempt' AND @Direction = 'desc' THEN R.Attempt END desc,
        CASE WHEN @Column = 'Attempt' THEN u.LastName END asc,
        CASE WHEN @Column = 'TotalScore' AND @Direction = 'asc' THEN R.TotalScore END asc,
        CASE WHEN @Column = 'TotalScore' AND @Direction = 'desc' THEN R.TotalScore END desc,
        CASE WHEN @Column = 'TotalScore' THEN u.LastName END asc,
        CASE WHEN @Column = 'Created' AND @Direction = 'asc' THEN L.Name END asc,
        CASE WHEN @Column = 'Created' AND @Direction = 'desc' THEN L.Name END desc,
        CASE WHEN @Column = 'Created' THEN u.LastName END asc
    ) AS ROWNUM,
    COUNT(*) OVER () AS TotalCount, C.Id AS 'ClassroomId', C.Name AS 'ClassroomName', U.Id AS 'UserId', U.LastName, U.FirstName, QT.Name AS 'QuestionType',
    R.TotalScore, R.ProfanitiesFound, R.HasGaming, R.Created, R.Response, R.Attempt, R.IsPassingScore,
    L.Name AS 'LessonName', P.PageNumber
    FROM Responses R
    INNER JOIN AspNetUsers U ON U.Id = R.UserId
    INNER JOIN ClassroomUsers CU ON CU.UserId = U.Id
    INNER JOIN Classrooms C ON C.Id = CU.ClassroomId
    INNER JOIN Schools S ON S.Id = C.SchoolId
    INNER JOIN Districts D ON D.Id = S.DistrictId
    INNER JOIN Questions Q ON Q.Id = R.QuestionId
    INNER JOIN QuestionTypes QT ON QT.Id = Q.QuestionTypeId
    INNER JOIN Pages P ON P.Id = Q.PageId
    INNER JOIN Lessons L ON L.Id = P.LessonId
    WHERE
    ( 
        (@Filter IS NULL OR U.LastName LIKE LOWER('%'+@Filter+'%'))
        OR (@Filter IS NULL OR U.FirstName LIKE LOWER('%'+@Filter+'%'))
        OR (@Filter IS NULL OR L.Name LIKE LOWER('%'+@Filter+'%'))
        OR (@Filter IS NULL OR QT.Name LIKE LOWER('%'+@Filter+'%'))
    )
    AND
    (
        U.Id  = CASE WHEN (@Student <> '' AND @Student IS NOT NULL) THEN @Student ELSE U.Id END AND
        D.Id  = CASE WHEN (@District <> '' AND @District IS NOT NULL) THEN @District ELSE D.Id END AND
        S.Id  = CASE WHEN (@School <> '' AND @School IS NOT NULL) THEN @School ELSE S.Id END AND
        C.Id  = CASE WHEN (@Classroom <> '' AND @Classroom IS NOT NULL) THEN @Classroom ELSE C.Id END AND
        QT.Id = CASE WHEN (@QuestionType <> '' AND @QuestionType IS NOT NULL) THEN @QuestionType ELSE QT.Id END AND
        Q.Id  = CASE WHEN (@Question <> '' AND @Question IS NOT NULL) THEN @Question ELSE Q.Id END AND
        L.Id  = CASE WHEN (@Lesson <> '' AND @Lesson IS NOT NULL) THEN @Lesson ELSE L.Id END AND
        R.HasGaming = CASE WHEN (@Gaming <> '' AND @Gaming IS NOT NULL) THEN @Gaming ELSE R.HasGaming END   
    )
    AND
    (
        --TODO: WOULD EVENTUALLY LIKE TO MAKE THESE FILTERS NOT REQUIRED
        R.TotalScore >= @FirstScore AND R.TotalScore <= @SecondScore
    )
    AND
    (
        CASE
            -- if @profanity filter is true then we want all with counter greater than 0
            WHEN @Profanity = '1' THEN R.ProfanitiesFound > '0'
            -- else if @profanty filter is false then we want all with counter equal to 0
            WHEN @Profanity = '0' THEN R.ProfanitiesFound = '0'
            -- if nothing was sent in then just show all
            WHEN (@Profanity IS NULL OR @Profanity = '') THEN R.ProfanitiesFound = R.Profanities
        END
    )

)

SELECT TotalCount, ClassroomId, ClassroomName, UserId, LastName, FirstName, QuestionType, TotalScore, ProfanitiesFound, HasGaming, Created, Response, Attempt, IsPassingScore, LessonName, PageNumber
FROM CTE_Results
WHERE ROWNUM > @FirstRec AND ROWNUM < @LastRec

2 个答案:

答案 0 :(得分:3)

您不需要CASE语句即可执行此操作,您可以直接在WHERE子句中执行此操作,就像这样:

WHERE 
(
  --OTHER FILTERS HERE
)
AND
(
    -- if @profanity filter is true then we want all with counter greater than 0
    (@Profanity = '1' AND R.ProfanitiesFound > '0')
    OR
    -- else if @profanty filter is false then we want all with counter equal to 0
    (@Profanity = '0' AND R.ProfanitiesFound = '0')
    OR
    -- if nothing was sent in then just show all
    ((@Profanity IS NULL OR @Profanity = '') AND R.ProfanitiesFound = R.Profanities)
)

答案 1 :(得分:1)

当出现以下情况时,您可以使用AND逻辑进行插入

OR( @Profanity = '1' AND R.ProfanitiesFound > '0')

OR( @Profanity = '0' AND R.ProfanitiesFound = '0')

OR((@Profanity IS NULL AND @Profanity = '') or R.ProfanitiesFound = R.Profanities)