Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL query for Unpivot

Status
Not open for further replies.

sds814

Programmer
Feb 18, 2008
164
US
I have a table structure (tblmapping) like the following:

TableName ColumnName ColumnValue
Product ProductID 1
Product ProductID 2
Product ProductName Keyboard
Product ProductName Mouse

I want to convert from column based data to row based data. I tried the following query, but syntax errors:

Code:
SELECT col,value
FROM
(SELECT DISTINCT ColumnName
FROM tblMapping
WHERE TableName = 'Product') p
UNPIVOT
(ColumnValue FOR ColumnName IN (SELECT DISTINCT ColumnName
								FROM tblMapping
								WHERE TableName = 'Product')
) AS unpvt;

Msg 156, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'SELECT'.
Msg 102, Level 15, State 1, Line 9
Incorrect syntax near ')'.

Not sure what I'm doing wrong. Thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top