-3

I'm trying to unpivot this table but I always get an error.

Does somebody a solution for this ?

The result has to be like this, 3 columns ID / question (q) / total number of these row

enter image description here

enter image description here

  • 2
    Where is the error ? Can you show us ? What is the expected result ? – Yogesh Sharma Apr 28 '20 at 09:02
  • 2
    Hi, [Tour], [ask] , and [mre]. Are your best bet for now in order to have an answerable question. You can simply build a simple example table in excel and the expected output. that will be the first step. You should really provide enought information to reproduce the issue [Why should I provide a Minimal Reproducible Example for a very simple SQL query?](https://meta.stackoverflow.com/questions/333952/why-should-i-provide-a-minimal-reproducible-example-for-a-very-simple-sql-query) – Drag and Drop Apr 28 '20 at 09:04
  • 3
    Please do **not** post code as images. See here for more details why: http://meta.stackoverflow.com/questions/285551 –  Apr 28 '20 at 09:09
  • 2
    Note that picture of code are not great. We can't copy past or anything. It's like showing pictures of your car to a mechanic. – Drag and Drop Apr 28 '20 at 09:10
  • My bad, I talked about Excel and you jumped on it. Missing 2 things the simplification of your data "minimal". For example I googled for pivot and took a [random question](https://stackoverflow.com/questions/10404348/sql-server-dynamic-pivot-query). The data is clear and understandable. In your question there is no obvious link between the 2 pictures dataset. The id doesn't match. 2nd the "no picture" policy. It can be incredibly painfull to hand type every data from the picture in order to craft an answer. But you have access to copy-past. – Drag and Drop Apr 28 '20 at 09:38

1 Answers1

0

You can use apply :

select cc.q2 as id_cc, cq.*
from src.qa_data_cc cc cross apply
     ( values (cc.q77, 'q77'), (cc.q78, 'q78'), 
              (cc.q79, 'q79'), (cc.q80, 'q80'),
               . . .
     ) cq(cols, colname);
Yogesh Sharma
  • 49,870
  • 5
  • 26
  • 52