Solved

T-SQL PIVOT:  Is there any way to do an 'all values' for columns instead of [1], [2], [3], ..., [200]

Posted on 2013-01-23
4
284 Views
Last Modified: 2013-01-28
Hi All

I have a table my_table that has a row_id, column_id, and value.
There are 500 rows and 200 columns, so rows x columns = a rowcount of 100,000.

To flatten all rows, and the first three columns, the below T-SQL works great:
SELECT row_id, [1], [2], [3]
FROM (SELECT row_id, column_id, value FROM my_table WHERE some_fk_id = 0 AND column_id IN (1, 2, 3)) AS src
PIVOT ( MIN(value) FOR column_id IN ([1], [2], [3])) AS tgt

Open in new window

I have 200 columns, and would like to avoid hard-typing [1], [2], [3], ..., [200] in my T-SQL.
Question:  Is there any way in a PIVOT to do an 'all values' for columns?

Thanks in advance.
Jim
0
Comment
Question by:Jim Horn
  • 3
4 Comments
 
LVL 17

Accepted Solution

by:
Barry Cunney earned 500 total points
ID: 38810392
SELECT  @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT
                                '],[' + t.ColumnName
                        FROM    #Table AS t
                        --ORDER BY '],[' + t.ID
                        FOR XML PATH('')
                      ), 1, 2, '') + ']'

SELECT  @cols

SET @query = N'SELECT ID,'+ @cols +' FROM
(SELECT t1.ID,t1.ColumnName , t1.Value FROM #Table AS t1) p
PIVOT (MAX([Value]) FOR ColumnName IN ( '+ @cols +' ))
AS pvt;'

EXECUTE(@query)
0
 
LVL 65

Author Comment

by:Jim Horn
ID: 38810668
... playing with it ...
0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 0 total points
ID: 38810874
Bingo bango.

Had to change a couple of things in the STUFF to pull this off.

-- TESTING ONLY, set to 0 for production, 1 for extra troubleshooting sets
DECLARE @debug bit = 1

DECLARE @cols VARCHAR(max), @query VARCHAR(max)

SELECT @cols = STUFF((   
	SELECT '[' + CAST(t.column_id as varchar(max)) + '],'
	FROM mrmd.comment_version AS t 
	WHERE cluster_id = 0 AND row_id = 1
	ORDER BY t.column_id
	FOR XML PATH('') ), 1, 0, '') 

SELECT @cols = LEFT(@cols, LEN(@cols) - 1)

IF @debug = 1
	SELECT 'cols', @cols

SET @query = N'
	SELECT row_id, '+ @cols +' 
	FROM ( SELECT row_id, column_id, value FROM mrmd.comment_version WHERE cluster_id = 0) as src 
	PIVOT ( Min(value) FOR column_id IN ( '+ @cols +' )) as pvt' 

IF @debug = 1
	SELECT '@query', @query
	
EXECUTE(@query)
GO

Open in new window

0
 
LVL 65

Author Closing Comment

by:Jim Horn
ID: 38826167
Thanks.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question