Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
I have the first table, is there any way to select from there and generate the second table output?[url=http://www.imagechicken.com][/url]Thank you very much.
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2010-02-03 : 04:18:27
yup. see below
DECLARE @List varchar(3000)SELECT @List = COALESCE(@List + '],[','') + ValFROM (SELECT A as Val FROM Table UNION SELECT B FROM Table UNION SELECT C FROM Table )tSET @SQL='SELECT *FROM (SELECT Key,Cat,ValFROM Table tUNPIVOT ( Val FOR Cat IN ([A],[B],[C]))u)mPIVOT (MAX(Cat) FOR Val IN ([' + @List + '])p'EXEC (@Sql)
pacolim
Starting Member
11 Posts
Posted - 2010-02-03 : 04:32:44
receive this error message:Incorrect syntax near 'p'.
pacolim
Starting Member
11 Posts
Posted - 2010-02-03 : 04:50:53
oh, it just missing one close bracketPIVOT (MAX(Cat) FOR Val IN ([' + @List + ']))p'It's working!! thanks a lot visakh16