0

I'm working on query to take the values from table

Id  QuestionNo  Response StartTime  EndTime
101 1           2        2015-04-21 2015-04-21
101 2           1        2015-04-21 2015-04-21
101 3           4        2015-04-21 2015-04-21
104 1           3        2015-04-21 2015-04-21
104 2           5        2015-04-21 2015-04-21
104 3           2        2015-04-21 2015-04-21

and I want to have the result like this

Id  Response          Response         Response         StartTime  EndTime
for QuestionNo=1  for QuestionNo=2 for QuestionNo=3 
101        2                1              4            2015-04-21 2015-04-21
104        3                5              2            2015-04-21 2015-04-21

I've tried some queries and with pivot, but it is to complex for me.Can anyone help me with this? Many thanks is advance.

Isav
  • 37
  • 1
  • 3
  • Are you using MySQL or SQL Server? They are *not* the same thing. – John Conde Apr 27 '15 at 12:46
  • I'm using Microsoft SQL Server 2012 – Isav Apr 27 '15 at 12:56
  • 1
    This question has been asked many times before. Search the site for [SQL Server Pivot](http://stackoverflow.com/search?q=sql+server+pivot) to get a comprehensive list of answers. – Dan Apr 27 '15 at 13:00
  • Yes, I've checked the site for SQL Server Pivot, but the examples are not helping me to much because I want query to be dynamically. On my table on the column QuestionNo I have values 1,2...10 and I need to check for values 1,2 and 3 and after display the responses that correspond to those values in separate columns. – Isav Apr 27 '15 at 13:21
  • What do you mean by "not helping me to much because I want query to be dynamically"? There must be lots of examples of *dynamic* pivot on the site. Have you tried looking for dynamic pivot specifically? – Andriy M Apr 28 '15 at 09:31
  • Since you are saying you've tried something, please show us your attempts, so that it would be clear what exactly you did wrong and so that you could get *help* rather than your job done for you. – Andriy M Apr 28 '15 at 09:42
  • 2
    Also, if you are trying to create a dynamic query from the very beginning, you are doing it wrongly. Do a working *static* version first. Do one with e.g. 2 columns, then do another one with 3, then compare the two versions to see where they differ and thus what parts would be dynamic, then try building the query dynamically as a string, look at that string, compare to your static queries, try executing it, look at the results... In short, just don't give up too early. – Andriy M Apr 28 '15 at 09:50
  • possible duplicate of [Efficiently convert rows to columns in sql server](http://stackoverflow.com/questions/15745042/efficiently-convert-rows-to-columns-in-sql-server) – jpw Apr 28 '15 at 11:18
  • It is working like this: select ID, MAX(IIF(QuestionNo = '1', Response, NULL)) Response1, MAX(IIF(QuestionNo = '2', Response, NULL)) Response2, MAX(IIF(QuestionNo = '3', Response, NULL)) Response3 FROM TableName GROUP BY ID – Isav Apr 28 '15 at 11:58

0 Answers0