﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / Pivot problem / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 23 May 2013 18:51:00 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Pivot problem</title><link>http://www.sqlservercentral.com/Forums/Topic1406313-392-1.aspx</link><description>[quote][b]SQL006 (1/14/2013)[/b][hr][code="sql"]CREATE TABLE #Test		(			ProductID int,			MainMasterFeatureID int,			--MasterFeatureValue Varchar(100),			ChilFeatureName varchar(100),			ParentFeatureName varchar(100)		)--Inserting Sample Data 		INSERT INTO #TestSELECT 40,1,'Pack','Type' UNION ALL SELECT 40,0,'Laminate','Technology'UNION ALLSELECT 40,11,'Yes','Coated'UNION ALLSELECT 52,1,'Roll','Type'UNION ALLSELECT 52,11,'NO','Coated'CREATE TABLE #tProduct    (        tProductID int PRIMARY KEY,         tProductCode nvarchar(128),         tProductName nvarchar(256)    )INSERT INTO #tProductSELECT 40,'001','ABC'UNION ALLSELECT 52,'002','XYZ'UNION ALLSELECT 50,'006','IJK'Declare @sql Varchar(MAX) Select @sql  = 'SELECT ProductID, ' +STUFF((Select DISTINCT ',MAX(Case When ParentFeatureName = ' + CHAR(39) + ParentFeatureName + CHAR(39) + ' Then ChilFeatureName Else ' + CHAR(39) + CHAR(39) + ' End) As ' + ParentFeatureName From #Test For XML PATH('')),1,1,'')   + ' FROM #Test Group By ProductID'   DECLARE @Product varchar(max)  SET @Product = '(SELECT *					FROM #tProduct) AS P JOIN'+@SQL+' AS T ON P.tProductID = T.ProductID'[/code]this error is coming..tried a lot [code="plain"]Msg 156, Level 15, State 1, Line 2Incorrect syntax near the keyword 'AS'.[/code] Now I want to join this dynamic query to #tProduct to get the desired results: [code="plain"]tProductID   tProductName        Type             Technology      Coated    40            ABC              Pack              Laminate          YES    52            XYZ              Roll               Null              No  [/code][/quote] The following is the edited version of your above sql. I've marked the changes that I made in bold. It gets you the required results : [code="sql"]Declare @sql Varchar(MAX) DECLARE @Product varchar(max)Select @sql  = 'SELECT ProductID, ' +STUFF((Select DISTINCT ',MAX(Case When ParentFeatureName = ' + CHAR(39) + ParentFeatureName + CHAR(39) + ' Then ChilFeatureName Else ' + CHAR(39) + CHAR(39) + ' End) As ' + ParentFeatureName From #Test For XML PATH('')),1,1,'')   + ' FROM #Test Group By ProductID' SET @Product = '[b]Select tProductId, tProductName, Type, Technology, Coated From [/b](SELECT * FROM #tProduct) AS P JOIN [b]([/b]'+@SQL+'[b])[/b] AS T ON P.tProductID = T.ProductID' Execute (@Product)[/code] If you still don't understand then print the variable @product for both the queries...yours and mine....you'll see the difference. Hope this worked out for you.</description><pubDate>Tue, 15 Jan 2013 02:21:25 GMT</pubDate><dc:creator>vinu512</dc:creator></item><item><title>RE: Pivot problem</title><link>http://www.sqlservercentral.com/Forums/Topic1406313-392-1.aspx</link><description>When I run the SQL you posted, I don't get that error at all.  Instead I get complaints that the objects in bold (fragment of your assignment to @SQL) don't exist:[code="sql"][b]ParentFeatureName[/b] + CHAR(39) + ' Then ChilFeatureName Else ' + CHAR(39) + CHAR(39) + ' End) As ' + [b]ParentFeatureName[/b] From [b]#Test[/b] [/code]Neither the column name nor the referenced table are provided in the DDL.Then of course there's the question of "what do you think you're assigning to @Product?"  It starts with an open paren.  Is that supposed to be a subquery or something?</description><pubDate>Tue, 15 Jan 2013 01:44:48 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Pivot problem</title><link>http://www.sqlservercentral.com/Forums/Topic1406313-392-1.aspx</link><description>[code="sql"]CREATE TABLE #Test		(			ProductID int,			MainMasterFeatureID int,			--MasterFeatureValue Varchar(100),			ChilFeatureName varchar(100),			ParentFeatureName varchar(100)		)--Inserting Sample Data 		INSERT INTO #TestSELECT 40,1,'Pack','Type' UNION ALL SELECT 40,0,'Laminate','Technology'UNION ALLSELECT 40,11,'Yes','Coated'UNION ALLSELECT 52,1,'Roll','Type'UNION ALLSELECT 52,11,'NO','Coated'CREATE TABLE #tProduct    (        tProductID int PRIMARY KEY,         tProductCode nvarchar(128),         tProductName nvarchar(256)    )INSERT INTO #tProductSELECT 40,'001','ABC'UNION ALLSELECT 52,'002','XYZ'UNION ALLSELECT 50,'006','IJK'Declare @sql Varchar(MAX) Select @sql  = 'SELECT ProductID, ' +STUFF((Select DISTINCT ',MAX(Case When ParentFeatureName = ' + CHAR(39) + ParentFeatureName + CHAR(39) + ' Then ChilFeatureName Else ' + CHAR(39) + CHAR(39) + ' End) As ' + ParentFeatureName From #Test For XML PATH('')),1,1,'')   + ' FROM #Test Group By ProductID'   DECLARE @Product varchar(max)  SET @Product = '(SELECT *					FROM #tProduct) AS P JOIN'+@SQL+' AS T ON P.tProductID = T.ProductID'[/code]this error is coming..tried a lot [code="plain"]Msg 156, Level 15, State 1, Line 2Incorrect syntax near the keyword 'AS'.[/code] Now I want to join this dynamic query to #tProduct to get the desired results: [code="plain"]tProductID   tProductName        Type             Technology      Coated    40            ABC              Pack              Laminate          YES    52            XYZ              Roll               Null              No  [/code]</description><pubDate>Mon, 14 Jan 2013 23:33:26 GMT</pubDate><dc:creator>SQL006</dc:creator></item><item><title>RE: Pivot problem</title><link>http://www.sqlservercentral.com/Forums/Topic1406313-392-1.aspx</link><description>[quote][b]SQL006 (1/14/2013)[/b][hr]thanks vinuNow i am trying to join this query with a table to get the desire results[code="sql"]	CREATE TABLE #tProduct		(			tProductID bigint PRIMARY KEY, 			tProductCode nvarchar(128), 			tProductName nvarchar(256), 			tManufacturerName nvarchar(256),			tProductDescription nvarchar(4000)		)EXECUTE ('SELECT * 		FROM #tProduct AS P		JOIN(SELECT ProductID,  +STUFF((Select DISTINCT '',MAX(Case When ParentFeatureName = '' + CHAR(39) + ParentFeatureName + CHAR(39) + '' Then ChilFeatureName Else '' + CHAR(39) + CHAR(39) + '' End) As '' + ParentFeatureName From #Test For XML PATH('')),1,1,'''')   +  FROM #Test Group By ProductID'' ) AS A ON P.tProductID = A.ProductID') [/code]I am getting this error[code="plain"]Msg 156, Level 15, State 1, Line 4Incorrect syntax near the keyword 'AS'.[/code][/quote]First suggestion I have is build the dynamic query into a variable.  Second, PRINT it out so you can see the query itself to be sure it is what you expect.  If you print it, you can copy it to another query window to test that it actually runs.  This is what I do when writing a dynamic query as it is sometimes hard to be sure you have the correct number of single quotes in the correct places otherwise.</description><pubDate>Mon, 14 Jan 2013 06:56:43 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Pivot problem</title><link>http://www.sqlservercentral.com/Forums/Topic1406313-392-1.aspx</link><description>thanks vinuNow i am trying to join this query with a table to get the desire results[code="sql"]	CREATE TABLE #tProduct		(			tProductID bigint PRIMARY KEY, 			tProductCode nvarchar(128), 			tProductName nvarchar(256), 			tManufacturerName nvarchar(256),			tProductDescription nvarchar(4000)		)EXECUTE ('SELECT * 		FROM #tProduct AS P		JOIN(SELECT ProductID,  +STUFF((Select DISTINCT '',MAX(Case When ParentFeatureName = '' + CHAR(39) + ParentFeatureName + CHAR(39) + '' Then ChilFeatureName Else '' + CHAR(39) + CHAR(39) + '' End) As '' + ParentFeatureName From #Test For XML PATH('')),1,1,'''')   +  FROM #Test Group By ProductID'' ) AS A ON P.tProductID = A.ProductID') [/code]I am getting this error[code="plain"]Msg 156, Level 15, State 1, Line 4Incorrect syntax near the keyword 'AS'.[/code]</description><pubDate>Mon, 14 Jan 2013 05:53:48 GMT</pubDate><dc:creator>SQL006</dc:creator></item><item><title>RE: Pivot problem</title><link>http://www.sqlservercentral.com/Forums/Topic1406313-392-1.aspx</link><description>I think this is what you are looking for...it would dynamically flatten your data : [code="sql"]--Creating Table CREATE TABLE #Test		(			ProductID int,			MainMasterFeatureID int,			--MasterFeatureValue Varchar(100),			ChilFeatureName varchar(100),			ParentFeatureName varchar(100)		)--Inserting Sample Data 		INSERT INTO #TestSELECT 40,1,'Pack','Type' UNION ALL SELECT 40,0,'Laminate','Technology'UNION ALLSELECT 40,11,'Yes','Coated'UNION ALLSELECT 52,1,'Roll','Type'UNION ALLSELECT 52,11,'NO','Coated'--Dynamic Cross Tab QueryDeclare @sql Varchar(MAX) Select @sql  = 'SELECT ProductID, ' Select @sql = @sql + STUFF((Select DISTINCT ',MAX(Case When ParentFeatureName = ' + CHAR(39) + ParentFeatureName + CHAR(39) + ' Then ChilFeatureName Else ' + CHAR(39) + CHAR(39) + ' End) As ' + ParentFeatureName From #Test For XML PATH('')),1,1,'') Select @sql = @sql + ' FROM #Test Group By ProductID' Execute(@sql) [/code] Hope this helps.</description><pubDate>Sun, 13 Jan 2013 23:04:38 GMT</pubDate><dc:creator>vinu512</dc:creator></item><item><title>RE: Pivot problem</title><link>http://www.sqlservercentral.com/Forums/Topic1406313-392-1.aspx</link><description>Then I suggest reading the following:[b][url]http://www.sqlservercentral.com/articles/T-SQL/63681/[/url][/b][b][url]http://www.sqlservercentral.com/articles/Crosstab/65048/[/url][/b]</description><pubDate>Sat, 12 Jan 2013 03:31:52 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Pivot problem</title><link>http://www.sqlservercentral.com/Forums/Topic1406313-392-1.aspx</link><description>Hi Lynn, thanks for the replyThe value in ParentFeatureName can be dynamically change..it will not  always give the same valueI am trying this dynamic pivot..but still not getting the desire results there is error[code="sql"][strike]DECLARE @ParentFeatureName varchar(max)SELECT @ParentFeatureName = STUFF((select distinct ',['+'ParentFeatureName'+']' from #Test for xml path(' ')),1,1,'')--error is coming here to get the distict value [/strike]SELECT @ParentFeatureName = (SELECT SUBSTRING(		(SELECT DISTINCT ',' + CAST(ParentFeatureName as Varchar(MAX)) FROM #Test FOR XML PATH('')),2,2000000)) [strike]SELECT ProductID,'+@ParentFeatureName+'FROM(SELECT * FROM #Test) AS DPIVOT(			MIN(MasterFeatureValue)			FOR  ParentFeatureName IN ('+@ParentFeatureName+') 		) AS DDGroup By ProductID[/strike]DECLARE @sql VARCHAR(max)SET @sql = 'SELECT ProductID,'+@ParentFeatureName+'FROM(SELECT * FROM #Test) AS DPIVOT(			MIN(MasterFeatureValue)			FOR  ParentFeatureName IN ('+@ParentFeatureName+') 		) AS DDGroup By ProductID'EXEC @sql[/code]</description><pubDate>Sat, 12 Jan 2013 02:28:16 GMT</pubDate><dc:creator>SQL006</dc:creator></item><item><title>RE: Pivot problem</title><link>http://www.sqlservercentral.com/Forums/Topic1406313-392-1.aspx</link><description>This is what I developed:[code="sql"]CREATE TABLE #Test		(			ProductID int,			MainMasterFeatureID int,			MasterFeatureValue Varchar(100),			ChilFeatureName varchar(100),			ParentFeatureName varchar(100)		);		INSERT INTO #TestSELECT 40,1,'1','Pack','Type' UNION ALL SELECT 40,0,'Laminate','Technology','Technology'UNION ALLSELECT 40,11,'1','Yes','Coated'UNION ALLSELECT 52,1,'1','Roll','Type'UNION ALLSELECT 52,11,'1','NO','Coated';select * from #Test;goselect    ProductID,    max(case when ParentFeatureName = 'Type'            then case when MainMasterFeatureID = 0 then MasterFeatureValue else ChilFeatureName end        end) as [Type],    max(case when ParentFeatureName = 'Technology'            then case when MainMasterFeatureID = 0 then MasterFeatureValue else ChilFeatureName end        end) as [Technology],    max(case when ParentFeatureName = 'Coated'            then case when MainMasterFeatureID = 0 then MasterFeatureValue else ChilFeatureName end        end) as [Coated]from    #Testgroup by    ProductID;godrop table #Test;go/*ProductID   Type    Technology      Coated40          Pack     Laminate         YES52          Roll       Null           No  */[/code]</description><pubDate>Sat, 12 Jan 2013 01:27:26 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>Pivot problem</title><link>http://www.sqlservercentral.com/Forums/Topic1406313-392-1.aspx</link><description>HiI have the following table[code="sql"]CREATE TABLE #Test		(			ProductID int,			MainMasterFeatureID int,			--MasterFeatureValue Varchar(100),			ChilFeatureName varchar(100),			ParentFeatureName varchar(100)		)				INSERT INTO #Test		SELECT 40,1,,'Pack','Type' 		UNION ALL 		SELECT 40,0,'Laminate','Technology'		UNION ALL		SELECT 40,11,'Yes','Coated'		UNION ALL		SELECT 52,1,'Roll','Type'		UNION ALL		SELECT 52,11,'NO','Coated'										SELECT * FROM #Test[/code]Expected Output[strike]If MainMasterFeatureID = 0 then select value from MasterFeatureValue ..here In case of Technology value is Laminate(select value from MasterFeatureValue)ELSESELECT Value from ChilFeatureName [/strike][code="plain"]ProductID   Type    Technology      Coated40          Pack     Laminate         YES52          Roll       Null           No  [/code]Thank you</description><pubDate>Sat, 12 Jan 2013 00:10:20 GMT</pubDate><dc:creator>SQL006</dc:creator></item></channel></rss>