Pivot Only 2 Columns of Data

I'm trying to pivot a single table that contains 7 columns but I really only need 2 of them.  One column is called ID and the other is PROFILE_ID which is the one I want to transpose/pivot into 7 new columns based on the values originally returned.   I know I can use a union but I'm trying to simply it if when I have other scenarios similar to this that have more values like 27 that needs to get transposed/pivoted.  I tried a pivot syntax but it didn't seem to work for me or at least I'm doing it wrong.  I only need the 'ID' and 'PROFILE_ID' column from the original table that needs to be pivoted or transposed.  Below is an example of the original output and what I need or expect to get.  Thanks in advance!

Original Output from select * dbo.Table
Original Output
Need Output in this Format
Expected Output
KANEDA 0149Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
DECLARE @Table TABLE
(
	ID				INT,
	Profile_ID		INT
);

INSERT @Table (ID, Profile_ID)
VALUES
	(245613, 13),
	(245613, 11),
	(245613, 9),
	(310007, 11),
	(310007, 12),
	(310007, 9),
	(310007, 8),
	(310007, 10),
	(310007, 13),
	(320014, 8);

SELECT ID, [8], [9], [10], [11], [12], [13], [14]
FROM
(
	SELECT *
	FROM @Table
) AS Src
PIVOT
(
	MAX(Profile_ID)
	FOR Profile_ID IN ([8], [9], [10], [11], [12], [13], [14])
) AS Pvt

Open in new window

Brian CroweDatabase AdministratorCommented:
Here is a dynamic version since I assume that would be your next request...

CREATE TABLE #Table
(
	ID				INT,
	Profile_ID		INT
);

INSERT #Table (ID, Profile_ID)
VALUES
	(245613, 13),
	(245613, 11),
	(245613, 9),
	(310007, 11),
	(310007, 12),
	(310007, 9),
	(310007, 8),
	(310007, 10),
	(310007, 13),
	(320014, 8);

DECLARE @ColList	NVARCHAR(4000),
	@SQL			NVARCHAR(4000);

SELECT @ColList =
	STUFF(
	(
		SELECT ',[' + CAST(Profile_ID AS NVARCHAR) + ']'
		FROM #Table
		GROUP BY Profile_ID
		ORDER BY Profile_ID
		FOR XML PATH('')
	), 1, 1, '');

SELECT @SQL = 
	'SELECT ID, ' + @ColList + 
	'FROM
	(
		SELECT *
		FROM #Table
	) AS Src
	PIVOT
	(
		MAX(Profile_ID)
		FOR Profile_ID IN (' + @ColList + ')
	) AS Pvt';

EXECUTE sp_executesql @SQL;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KANEDA 0149Author Commented:
Thank you Brian, that is exactly what I needed and it worked perfectly!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.