I'm looking for a pure-SQL way (SQL Server 2012 if it matters) to convert row data to columns. I've tried searching for this, and can't find anything for the data format I'm trying to convert, possibly because my SQL knowledge is pretty basic.
My input data is a list of Release Names and Story Points extracted from our JIRA server, along with the extract date. The table I'm trying to use will actually contain extracts from many different projects, although I'm excluding the ProjectName column from these examples.
Input Data:
Version Date StoryPoints
1.0 2017-01-01 10
1.0 2017-02-01 10
1.0 2017-03-01 15
1.0 2017-04-01 15
2.0 2017-01-01 10
2.0 2017-02-01 10
2.0 2017-03-01 10
2.0 2017-04-01 10
3.0 2017-01-01 5
3.0 2017-02-01 5
3.0 2017-03-01 5
3.0 2017-04-01 5
Completed 2017-01-01 0
Completed 2017-02-01 5
Completed 2017-03-01 15
Completed 2017-04-01 28
We need to generate a table in either format below, which will be used to create a "burnup" chart in our Confluence wiki. Since each project can have different Version names, we can't hard-code any of the column names below (although "Completed" will exist in all projects).
Also, even though we will standardize on dates similar to ones in the example, I would prefer to not hard-code any of the date values into the query either.
Output Format #1:
Date 1.0 2.0 3.0 Completed
2017-01-01 10 10 5 0
2017-02-01 10 10 5 5
2017-03-01 15 10 5 15
2017-04-01 15 10 5 28
I recognize that it may be difficult to structure the data that way, so it's possible to use the following output format too, which I can also chart in Confluence (although it's not as intuitive as the above format).
Output Format #2:
Versions 2017-01-01 2017-02-01 2017-03-01 2017-04-01
1.0 10 10 15 15
2.0 10 10 10 10
3.0 5 5 5 5
Completed 0 5 15 28
Any help is GREATLY appreciated!
SQL Server 2012 requires you to hard code one of the dimensions you are pivoting by into the PIVOT query.
One way you could get around it is by building and executing a dynamic query string.
As suggested by Xingzhou Liu, I came up into this.
Source:
Using STUFF and FOR XML you can get the column dynamically
Then Pivot and Execute it as a command query string.
Results