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
280 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
Comment Utility
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
Comment Utility
... playing with it ...
0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 0 total points
Comment Utility
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
Comment Utility
Thanks.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now