Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using if else block in pivot query

Tags:

sql

sql-server

I have a table

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

I wrote a query like

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

Which gives an output of students who have less than 30 marks in 2 subjects as fail or else pass

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

I want to add 7 marks to each subject which is less than 30 and see the pass fail students after adding 7 marks. eg-After adding 7 marks rajesh record should be like

Rajesh 34 56 19 45 30 170 pass

like image 917
Ultron_111 Avatar asked May 11 '26 23:05

Ultron_111


1 Answers

Maybe this is something you're looking for:

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

I removed you're comparison logic that was for all the failure combinations and replaced it with sum + group by + case from the original table, so that you can determine the counts for fails, near passes and passes for each student without having to list all the cases separately.

You can test this in SQL Fiddle

like image 78
James Z Avatar answered May 14 '26 12:05

James Z