0

Sql server DB has a table like

Input data:

Indicator name  Jan-17  Feb-17  Mar-17
A               5       2       9
B               0       5       3
C               9       6       3
D               9       1       8

I want to see the output as follows, how do I do that in sql server?

Indicator Name  Date    Value
A               Jan-17  5
A               Feb-17  2
A               Mar-17  9
B               Jan-17  0
B               Feb-17  5
B               Mar-17  3
C               Jan-17  9
C               Feb-17  6
C               Mar-17  3
D               Jan-17  9
D               Feb-17  1
D               Mar-17  8
Giorgos Betsos
  • 68,064
  • 7
  • 48
  • 83
SwapnilH
  • 1
  • 1
  • You can use `UNPIVOT` operator. There are plenty of examples here in SO that demonstrate how you can unpivot a table. – Giorgos Betsos Jul 14 '17 at 06:10
  • Thanks it helps, but name of the columns keep on changing. e.g. if the data uploaded is for the month of June-2017 (Upload date) then it will have data from June-2016 until June-2017, similarly when it is uploaded lets say Aug-2017 then it will be from Aug-2016 until Aug-2017, is there any way to dynamically replace the "Jun-2016"and ""July-2016" in the query below, select u.INDICATOR_GROUP_NAME, u.date, u.value from xyz s unpivot ( value for date in ("Jun-2016", "Jul-2016") ) u; – SwapnilH Jul 14 '17 at 06:17
  • in that case you have to use *dynamic sql*. There are also plenty of relevant examples here in SO. Here's a [link](https://stackoverflow.com/questions/18026236/sql-server-columns-to-rows) to one of these. – Giorgos Betsos Jul 14 '17 at 06:20

0 Answers0