I have to create a query to find the count of 3s,2s and 1s for each unique FacName,course,section, term.
So the final output will have fields
facname,course,section,term,
Resp#1_A,Resp#1_N,Resp#1_D,
Resp#2_A,Resp#2_N,Resp#2_D,
Resp#3_A,Resp#3_N,Resp#3_D
Where for example Resp#1_A ==> Number of 3s for Resp#1 for that unique facname,course,section,term combination. Similarly Resp#1_N ==> Number of 2s for Resp#1 and Resp#3_D ==> number of 1s for Resp#3
I created a temporary table with the destination fields used a cursor to go pass each record and update the temp table to produce the final output. But because the table has millions of records it is taking a lot of time.
Is there any means by which this could be done with a normal query by using Joins and derived tables.
insert into Eval select 'Daniel','ME','001','156','3','3','1'
insert into Eval select 'Mark','PHY','012','156','2','3',NULL
insert into Eval select 'Antony','CHEM','001','156','3','1','3'
insert into Eval select 'Mark','PHY','012','156','3','3','2'
insert into Eval select 'Daniel','ME','001','156','1','2','1'
--select * from eval
SELECT facname, max(course) as course ,max(section) as section ,max(term) as term,
sum(CASE Resp#1 WHEN 3 THEN 1 ELSE 0 END) AS Resp#1_A,
sum(CASE Resp#1 WHEN 2 THEN 1 ELSE 0 END) AS Resp#1_N,
sum(CASE Resp#1 WHEN 1 THEN 1 ELSE 0 END) AS Resp#1_D,
sum(CASE Resp#2 WHEN 3 THEN 1 ELSE 0 END) AS Resp#2_A,
sum(CASE Resp#2 WHEN 2 THEN 1 ELSE 0 END) AS Resp#2_N,
sum(CASE Resp#2 WHEN 1 THEN 1 ELSE 0 END) AS Resp#2_D,
sum(CASE Resp#3 WHEN 3 THEN 1 ELSE 0 END) AS Resp#3_A,
sum(CASE Resp#3 WHEN 2 THEN 1 ELSE 0 END) AS Resp#3_N,
sum(CASE Resp#3 WHEN 1 THEN 1 ELSE 0 END) AS Resp#3_D
FROM Eval
GROUP BY facname
SELECT facname, course,section,term,
sum(CASE Resp#1 WHEN 3 THEN 1 ELSE 0 END) AS Resp#1_A,
sum(CASE Resp#1 WHEN 2 THEN 1 ELSE 0 END) AS Resp#1_N,
sum(CASE Resp#1 WHEN 1 THEN 1 ELSE 0 END) AS Resp#1_D,
sum(CASE Resp#2 WHEN 3 THEN 1 ELSE 0 END) AS Resp#2_A,
sum(CASE Resp#2 WHEN 2 THEN 1 ELSE 0 END) AS Resp#2_N,
sum(CASE Resp#2 WHEN 1 THEN 1 ELSE 0 END) AS Resp#2_D,
sum(CASE Resp#3 WHEN 3 THEN 1 ELSE 0 END) AS Resp#3_A,
sum(CASE Resp#3 WHEN 2 THEN 1 ELSE 0 END) AS Resp#3_N,
sum(CASE Resp#3 WHEN 1 THEN 1 ELSE 0 END) AS Resp#3_D
FROM Eval
GROUP BY facname, course,section,term