é uma forma de fazer!!!
create table #Gabarito
(
idGabarito integer,
idProva integer,
resposta01 char(1),
resposta02 char(1),
resposta03 char(1),
resposta04 char(1),
resposta05 char(1)
)
create table #Prova
(
idProva integer,
NomeProva varchar(30)
)
create table #RespostaCandidato
(
idCandidato integer,
idProva integer,
resposta01 char(1),
resposta02 char(1),
resposta03 char(1),
resposta04 char(1),
resposta05 char(1)
)
set nocount on
insert into #Prova (idProva, NomeProva) values (1, 'PROVA 01')
insert into #Gabarito (idGabarito, idProva, resposta01, resposta02, resposta03, resposta04, resposta05) values (1, 1, 'A', 'B', 'C', 'D', 'E')
insert into #RespostaCandidato (idCandidato, idProva, resposta01, resposta02, resposta03, resposta04, resposta05) values (1, 1, 'A', 'B', 'C', 'D', 'E')
insert into #RespostaCandidato (idCandidato, idProva, resposta01, resposta02, resposta03, resposta04, resposta05) values (2, 1, 'A', 'C', 'D', 'D', 'E')
insert into #RespostaCandidato (idCandidato, idProva, resposta01, resposta02, resposta03, resposta04, resposta05) values (3, 1, 'A', 'C', 'C', 'D', 'E')
select
idProva,
NomeProva,
idCandidato,
(acertou01 + acertou02 + acertou03 + acertou04 + acertou05) TotalAcertos
From
(
select
#Prova.idProva,
#Prova.NomeProva,
#RespostaCandidato.idCandidato,
Case
when #RespostaCandidato.resposta01 = #Gabarito.resposta01 then 1
else 0
End acertou01,
Case
when #RespostaCandidato.resposta02 = #Gabarito.resposta02 then 1
else 0
End acertou02,
Case
when #RespostaCandidato.resposta03 = #Gabarito.resposta03 then 1
else 0
End acertou03,
Case
when #RespostaCandidato.resposta04 = #Gabarito.resposta04 then 1
else 0
End acertou04,
Case
when #RespostaCandidato.resposta05 = #Gabarito.resposta05 then 1
else 0
End acertou05
from #RespostaCandidato
Inner Join #Prova on #Prova.idProva = #RespostaCandidato.idProva
Inner Join #Gabarito on #Gabarito.idProva = #Prova.idProva
)a
drop table #RespostaCandidato
drop table #Prova
drop table #Gabarito