Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Transpose a resultset from SQL

I am using Microsoft SQL Server 2008.
I have a table that looks something like this:

|======================================================|
| RespondentId | QuestionId | AnswerValue | ColumnName | 
|======================================================|
|     P123     |      1     |     Y       | CanBathe   |
|------------------------------------------------------|
|     P123     |      2     |     3       | TimesADay  |
|------------------------------------------------------|
|     P123     |      3     |     1.00    | SoapPrice  |
|------------------------------------------------------|
|     P465     |      1     |     Y       | CanBathe   |
|------------------------------------------------------|
|     P465     |      2     |     1       | TimesADay  |
|------------------------------------------------------|
|     P465     |      3     |     0.99    | SoapPrice  |
|------------------------------------------------------|
|     P901     |      1     |     N       | CanBathe   |
|------------------------------------------------------|
|     P901     |      2     |     0       | TimesADay  |
|------------------------------------------------------|
|     P901     |      3     |     0.00    | SoapPrice  |
|------------------------------------------------------|

I would like to flip the rows to be columns so that this table looks like this:

|=================================================|
| RespondentId | CanBathe | TimesADay | SoapPrice | 
|=================================================|
|     P123     |     Y    |      3    |   1.00    | 
|-------------------------------------------------|
|     P465     |     Y    |      1    |   0.99    | 
|-------------------------------------------------|
|     P901     |     N    |      0    |   0.00    | 
|-------------------------------------------------|

(the example data here is arbitrarily made up, so its silly)

The source table is a temp table with approximately 70,000 rows.
What SQL would I need to write to do this?

Update

  • I don't even know if PIVOT is the right way to go.
  • I don't know what column to PIVOT on.
  • The documentation mentions <aggregation function> and <column being aggregated> and I don't want to aggregate anything.

Thanks in advance.

like image 922
funkymushroom Avatar asked Aug 09 '11 19:08

funkymushroom


People also ask

How do I transpose a query result in mysql?

If you want to transpose only select row values as columns, you can add WHERE clause in your 1st select GROUP_CONCAT statement. If you want to filter rows in your final pivot table, you can add the WHERE clause in your SET statement.

Which technique is used to transpose data in SQL?

Another solution of transposing rows into columns is by using XML. The XML solution to transposing rows into columns is basically an optimal version of the PIVOT in that it addresses the dynamic column limitation.


2 Answers

It, is required to use an aggregate function if you use PIVOT. However, since your (RespondentId, QuestionId) combination is unique, your "groups" will have only one row, so you can use MIN() as an aggregate function:

SELECT RespondentId, CanBathe, TimesADay, SoapPrice
FROM (SELECT RespondentId, ColumnName, AnswerValue FROM MyTable) AS src
PIVOT (MIN(AnswerValue) FOR ColumnName IN(CanBathe, TimesADay, SoapPrice)) AS pvt

If a group only contain one row, then MIN(value) = value, or in other words: the aggregate function becomes the identity function.

like image 197
Elian Ebbing Avatar answered Oct 07 '22 03:10

Elian Ebbing


See if this gets you started. Used to have to use CASE statements to make that happen but it looks like some inkling of PIVOT is in SQL Server now.

like image 36
n8wrl Avatar answered Oct 07 '22 02:10

n8wrl