在数据透视查询中使用if else块

时间:2015-06-13 04:25:50

标签: sql sql-server

我有一张桌子

StudentID StudentName Subject Marks
      1   Savita      EC1     50
      1   Savita      EC2     55
      1   Savita      EC3     45
      1   Savita      EC4     34
      1   Savita      EC5     23
      2   Rajesh      EC1     34
      2   Rajesh      EC2     56
      2   Rajesh      EC3     12
      2   Rajesh      EC4     45
      2   Rajesh      EC5     23
      3   Smita       EC1     76
      3   Smita       EC2     45
      3   Smita       EC3     67
      3   Smita       EC4     56
      3   Smita       EC5     76
      4   Rahul       EC1     66
      4   Rahul       EC2     34
      4   Rahul       EC3     22
      4   Rahul       EC4     18
      4   Rahul       EC5     33

我写了一个像

这样的查询
SELECT StudentName, EC1,EC2,EC3,EC4,EC5,TotalMarks, case  
  when EC1<30 and ec2<30 then 'fail'
  when EC1<30 and EC3<30 then 'fail'
  when EC1<30 and EC4<30 then 'fail'
  when EC1<30 and EC5<30 then 'fail'
  when EC2<30 and EC3<30 then 'fail'
  when EC2<30 and EC4<30 then 'fail'
  when EC2<30 and EC5<30 then 'fail'
  when EC3<30 and EC4<30 then 'fail'
  when EC3<30 and EC5<30 then 'fail'
  when EC4<30 and EC5<30 then 'fail'
  else 'pass'
  end as Result
FROM (SELECT StudentName, EC1, EC2, EC3, EC4, EC5, TotalMarks=EC1+EC2+EC3+EC4+EC5
      FROM Student
      PIVOT(sum(Marks) for subject in([EC1],[EC2],[EC3],[EC4],[EC5],[TotalMarks]))as pivotTable) A

其中2个科目中少于30分的学生输出失败或传递

Rahul   66  34  22  18  33  173 fail
Rajesh  34  56  12  45  23  170 fail
Savita  50  55  45  34  23  207 pass
Smita   76  45  67  56  76  320 pass

我想为每个小于30的主题添加7个标记,并在添加7个标记后查看通过失败的学生。例如 - 添加7个标记后,rajesh记录应该像

Rajesh 34 56 19 45 30 170 pass

3 个答案:

答案 0 :(得分:4)

也许这是你正在寻找的东西:

SELECT A.StudentName, EC1,EC2,EC3,EC4,EC5,Total,
  case when fail2 >= 2 then 'Failure'
       when fail >= 2 then 'Near Pass'
       else 'Pass' end as Result
FROM
(  SELECT StudentName, EC1, EC2, EC3, EC4, EC5
      FROM Student
      PIVOT(sum(Marks) for subject in([EC1],[EC2],[EC3],[EC4],[EC5]))as pt) A,
(  select
      studentName,
      sum(case when Marks <  30 then 1 else 0 end) as fail,
      sum(case when Marks <  23 then 1 else 0 end) as fail2,
      sum(case when Marks >= 30 then 1 else 0 end) as pass,
      sum(marks) as total
  from
      student
  group by
      studentname
) B
where 
    A.StudentName = B.StudentName

我删除了你所有失败组合的比较逻辑,并将其替换为来自原始表的sum + group by + case,以便您可以确定每个失败,近通道和通过的计数学生无需单独列出所有案例。

您可以在SQL Fiddle

中对此进行测试

答案 1 :(得分:2)

您可以使用SUMCOUNT聚合函数的窗口版本来简化查询:

SELECT StudentID, StudentName, 
       [EC1],[EC2],[EC3],[EC4],[EC5], 
       Total,
       CASE WHEN Below30 >= 2 THEN 'Fail'
            ELSE 'Pass'
       END AS WithoutBonus, 
       CASE WHEN Below23 >= 2 THEN 'Fail'
            ELSE 'Pass'
       END AS WithBonus
FROM (SELECT StudentID, StudentName, Subject, Marks,
             SUM(Marks) OVER (PARTITION BY StudentID) AS Total,
             COUNT(CASE WHEN Marks < 30 THEN 1 END) 
                OVER (PARTITION BY StudentID) AS Below30, 
             COUNT(CASE WHEN Marks < 23 THEN 1 END) 
                OVER (PARTITION BY StudentID) AS Below23 
      FROM mytable ) AS src
PIVOT (
   MAX (Marks)
   FOR Subject IN ([EC1],[EC2],[EC3],[EC4],[EC5]) ) pvt
ORDER BY StudentName

Below30计算具有<1的Mark的受试者的数量。每个StudentID 30 Below23计算具有<1的Mark的受试者的数量。 23 per StudentID

因此,WithoutBonus'Fail' ,如果特定学生有2个或更多主题Mark<30,而WithBonus如果特定学生有2个或更多主题'Fail',则为Mark<23

Demo here

如果您希望输出Marks值增加7,如果它们是< 30,并且您不关心WithoutBonus估算值,那么您可以使用以下内容版本:

SELECT StudentID, StudentName, 
       [EC1],[EC2],[EC3],[EC4],[EC5], 
       Total,
       CASE WHEN Below23 >= 2 THEN 'Fail'
            ELSE 'Pass'
       END AS WithBonus
FROM (SELECT StudentID, StudentName, Subject, 
             CASE WHEN Marks < 30 THEN Marks+7 ELSE Marks END AS Marks,
             SUM(Marks) OVER (PARTITION BY StudentID) AS Total,          
             COUNT(CASE WHEN Marks < 23 THEN 1 END) OVER (PARTITION BY StudentID) AS Below23 
      FROM mytable ) AS src
PIVOT (
   MAX (Marks)
   FOR Subject IN ([EC1],[EC2],[EC3],[EC4],[EC5]) ) pvt
ORDER BY StudentName

<强>输出:

StudentID   StudentName EC1 EC2 EC3 EC4 EC5 Total   WithBonus
--------------------------------------------------------------
4           Rahul       66  34  29  25  33  173     Fail
2           Rajesh      34  56  19  45  30  170     Pass
1           Savita      50  55  45  34  30  207     Pass
3           Smita       76  45  67  56  76  320     Pass

答案 2 :(得分:1)

你可以试试这个

SELECT 
    StudentName,
    CASE WHEN EC1 < 30 THEN EC1 + 7 ELSE EC1 END AS EC1,
    CASE WHEN EC2 < 30 THEN EC2 + 7 ELSE EC2 END AS EC2,
    CASE WHEN EC3 < 30 THEN EC3 + 7 ELSE EC3 END AS EC3,
    CASE WHEN EC4 < 30 THEN EC4 + 7 ELSE EC4 END AS EC4,
    CASE WHEN EC5 < 30 THEN EC5 + 7 ELSE EC5 END AS EC5,
    Total = (EC1 + EC2 + EC3 + EC4 + EC5),
    CASE  
       WHEN EC1 < 23 AND EC2 < 23 THEN 'FAIL'
       WHEN EC1 < 23 AND EC3 < 23 THEN 'FAIL'
       WHEN EC1 < 23 AND EC4 < 23 THEN 'FAIL'
       WHEN EC1 < 23 AND EC5 < 23 THEN 'FAIL'
       WHEN EC2 < 23 AND EC3 < 23 THEN 'FAIL'
       WHEN EC2 < 23 AND EC4 < 23 THEN 'FAIL'
       WHEN EC2 < 23 AND EC5 < 23 THEN 'FAIL'
       WHEN EC3 < 23 AND EC4 < 23 THEN 'FAIL'
       WHEN EC3 < 23 AND EC5 < 23 THEN 'FAIL'
       WHEN EC4 < 23 AND EC5 < 23 THEN 'FAIL'
       ELSE 'PASS'
   END AS Result
FROM
(
   SELECT * FROM Student
) AS ST
PIVOT
(
    SUM(Marks) For [Subject] IN (EC1, EC2, EC3, EC4, EC5)
) AS PV

<强>输出

Rahul   66  34  29  25  33  173 FAIL
Rajesh  34  56  19  45  30  170 PASS
Savita  50  55  45  34  30  207 PASS
Smita   76  45  67  56  76  320 PASS