Selectselect top 100 * From
from (
select codCliente
,Resposta
Select codCliente, Resposta ,NrQuestao --,COUNT(1) as Qtde
,(
,(Select select COUNT(1) as qtde
From from tblRespostaINT T2
Where where T2.codCliente = T0.codCliente
And and T2.codNrQuestao between 9
and 29) as QResp
From tblRespostaINTand T029
Inner) as JoinQResp
tblQuestao T1 On T0.codNrQuestaofrom =tblRespostaINT T1.codNrQuestaoT0
inner join tblQuestao T1 on T0.codNrQuestao = T1.codNrQuestao
Wherewhere T0.CodQuestionario = 1
and T1.codNrQuestao between 9
And T1.codNrQuestao between 9 and 29
--and codCliente = 5156
--Group by codCliente, Resposta,NrQuestao
) as tblx
Pivot(Sum(resposta) for NrQuestao in (
as tblx [I_1_P14_1]
Pivot (Sum(resposta) for NrQuestao in ([I_1_P14_1] ,[I_2_P14_1]
,[I_3_P14_1]
,[I_4_P14_1]
,[I_5_P14_1],[I_6_P14_1],[I_7_P14_1],
,[I_6_P14_1]
,[I_7_P14_1]
,[I_9_P14_1]
[I_9_P14_1] ,[I_10_P14_1]
,[I_11_P14_1]
,[I_12_P14_1]
,[I_13_P14_1],[I_14_P14_1],[I_15_P14_1],
,[I_14_P14_1]
,[I_15_P14_1]
,[I_16_P14_1]
[I_16_P14_1],[I_17_P14_1]
,[I_18_P14_1]
,[I_19_P14_1]
,[I_20_P14_1]
,[I_21_P14_1]
,[I_22_P14_1])
)) tbl
Select select codCliente,
,Resposta
,NrQuestao --,COUNT(1) as Qtde
,(
select COUNT(1) as qtde
,(Select COUNT(1) as qtde Fromfrom tblRespostaINT T2
Where where T2.codCliente = T0.codCliente
And T2.codNrQuestao between 9 and 29) as QResp
and T2.codNrQuestao between 9
From tblRespostaINTand T029
) as QResp
from InnertblRespostaINT T0
inner Joinjoin tblQuestao T1 Onon T0.codNrQuestao = T1.codNrQuestao
Wherewhere T0.CodQuestionario = 1
Andand T1.codNrQuestao between 9
and 29
and 29
--and codCliente = 5156
--Group by codCliente, Resposta,NrQuestao
Query PlanQuery Plan: