Prefix column names when using dynamic Pivot
NickName:webworm Ask DateTime:2016-10-14T02:21:20

Prefix column names when using dynamic Pivot

So I am working on a way to report some data from a table that tracks patients response to questions about chest pain when they visit the emergency room. When patients arrive to the ER they are asked a series of questions regarding their chest pain. For each question they are asked to rank the pain from 1 to 10. Below is the data as it is stored in the table.

+-----------+------------+----------+
| PatientId | QuestionId | PainRank |
+-----------+------------+----------+
|     1     |      1     |     5    |
+-----------+------------+----------+
|     1     |      2     |     6    |
+-----------+------------+----------+
|     1     |      3     |     4    |
+-----------+------------+----------+
|     2     |      1     |     1    |
+-----------+------------+----------+
|     2     |      2     |     8    |
+-----------+------------+----------+
|     2     |      3     |     2    |
+-----------+------------+----------+
|     3     |      1     |     5    |
+-----------+------------+----------+
|     3     |      2     |     4    |
+-----------+------------+----------+
|     3     |      3     |     7    |
+-----------+------------+----------+

I have created a dynamic Pivot of the data which changes the questions to columns as such so that physicians can look at a summary of a group a patients.

+-----------+---+---+---+
| PatientId | 1 | 2 | 3 |
+-----------+---+---+---+
|     1     | 5 | 6 | 4 |
+-----------+---+---+---+
|     2     | 1 | 8 | 2 |
+-----------+---+---+---+
|     3     | 5 | 4 | 7 |
+-----------+---+---+---+

What I would like to do is prefix the question numbers with the word "Question" so the columns will look like "Question - 1", "Question - 2", "Question - 3". I would like to add the prefix using the SQL query that created the dynamic pivot but since the Pivot is dynamic and am not sure how to add it.

Here is the SQL I used to obtain the dynamic Pivot.

  -- Dynamic Pivot
DECLARE @PivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnNameCollection AS NVARCHAR(MAX)

-- Get column list. A DISTINCT list of questions
SELECT @ColumnNameCollection= ISNULL(@ColumnNameCollection + ',','') 
       + QUOTENAME(QuestionId)
FROM (SELECT DISTINCT QuestionId FROM PatientChestPain) AS Questions

-- Build PIVOT query using the list of questions
SET @PivotQuery = 
  N'SELECT PatientId,  ' + @ColumnNameCollection + '
    FROM PatientChestPain
    PIVOT(MAX(PainRank) 
          FOR QuestionId IN (' + @ColumnNameCollection + ')) AS PivotTableResult'
--Execute the PIVOT query
EXEC sp_executesql @PivotQuery

I do realize I could just edit the final report and add "Question - ", however I am trying to generate the report automatically and I can only manipulate the SQL.

NOTE: I used the following website to generate the ascii tables. Very nice interface and easy to use. I tried creating a SQL Fiddle however it appears as though it is not working with SQL Server at the moment.

Copyright Notice:Content Author:「webworm」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40028251/prefix-column-names-when-using-dynamic-pivot

More about “Prefix column names when using dynamic Pivot” related questions

Prefix column names when using dynamic Pivot

So I am working on a way to report some data from a table that tracks patients response to questions about chest pain when they visit the emergency room. When patients arrive to the ER they are ask...

Show Detail

SQL Pivot Issue, Need dynamic column names

Alright. So we needed to get output as down with the code below. I got most of the code from: Convert Rows to columns using 'Pivot' in SQL Server. This gives me the dynamic column names ...

Show Detail

Create pivot table with dynamic column names

I am creating a pivot table which represents crash values for particular year. Currently, i am doing a hard code for column names to create pivot table. Is there anyway to make the column names dyn...

Show Detail

change column names in a dynamic pivot table result

I have a tsql dynamic pivot table query that works fine although I´m not happy with the column names in the final output. To make it work, the user has to select up to 20 fruit names from a list o...

Show Detail

how to preserve column names on dynamic pivot

Sales data contains dynamic product names which can contian any characters. Dynamic pivot table is created based on sample from Crosstab with a large or undefined number of categories translate(...

Show Detail

Dynamic pivot with similar column names

I'm looking for a way to pivot a varying amount of rows to columns in sql server 2008 R2. I created the data column RANK in the query because, ultimately I want the pivoted column names to be labe...

Show Detail

Adding prefix to column names generated by pivot_longer names_to .value

Is it possible to add a prefix to the column names that are generated from using the .value option in the argument names_to in pivot_longer ? Example: df <- tibble(x1 = 0.1, x2 = 0.2, y1 = 0.05,...

Show Detail

Dynamic PIVOT with Numbered Column Names

I'm getting the following when trying to pivot a table: Country Birmingham Dallas New Delhi --------------------------------------- India NULL NULL New Delhi UK Birmingham N...

Show Detail

SQL Server Dynamic Pivot Column Names

For the necessity of my application, I must return the column names of a query as the very first row. Now I must PIVOT this result in order to UNION it with my result set, but the difficult part is...

Show Detail

Dynamic column names in dynamic pivot query

I have created a report for our Sales department, and they have what (on the surface) appears to be a simple request: if a column name contains "date_WeeklySales2017", drop the "date_" portion of the

Show Detail