How do I transform some rows into columns?

0

I have this database with questions and answers

Youneedtocreateaviewthatthequestionsandanswersgetarrangedthatway.

.

A friend spoke of such a pivot but I can not make it work

    
asked by anonymous 06.08.2018 / 05:51

1 answer

2
  

I have this database with questions and answers
  You need to create a view that the questions and answers get set up like that.

Lucas, was not able to tell us what database manager you are using.

As the number of questions and answers is fixed, this is a suggestion that uses the classic pivot:

-- código #1
SELECT id_entrevistado, 
       1 as [pergunta 1], 
       max(case when id_pergunta = 1 then resposta end) as [resposta 1],
       2 as [pergunta 2], 
       max(case when id_pergunta = 2 then resposta end) as [resposta 2],
       ... acrescentar bloco de 3 a 11
       12 as [pergunta 12], 
       max(case when id_pergunta = 12 then resposta end) as [resposta 12]
  from Perguntas_Respostas
  group by id_entrevistado;

Supplement the code for questions 3 through 11; just copy the block below, replacing n with the question / answer number.

   n as [pergunta n], 
   max(case when id_pergunta = n then resposta end) as [resposta n],
    
06.08.2018 / 20:57