Need help pivoting some data?

Create table #attributes (ProductCode int, AttributeName varchar(20), AttributeValue int) insert into #attributes values (1, 'AttributeA', 10) insert into #attributes values (1, 'AttributeB', 20) insert into #attributes values (2, 'AttributeA', 30) insert into #attributes values (2, 'AttributeB', 40) insert into #attributes values (3, 'AttributeA', 50) declare @attributes_columns nvarchar(max) set @attributes_columns = ( select ', ' + AttributeName + '' from ( select distinct AttributeName as AttributeName from #attributes ) t order by t. AttributeName for xml path('') ) set @attributes_columns = stuff(@attributes_columns,1,2,'') declare @sql nvarchar(max) set @sql = N' select ProductCode, from (select ProductCode, AttributeName, AttributeValue from #attributes )p pivot ( sum(AttributeValue) for AttributeName in () ) as pvt ' set @sql = replace(@sql, '', @attributes_columns) print @sql exec sp_executesql @sql drop table #attributes.

Thank you very much! I plugged this in, and it worked great - except for two runtime errors: (1) the inserts and (2) initializing a local variable when declared. I'm using SQL2005, so I'm guessing that's the reason.Do you know of any way of using this logic inside of a table function?

I played around with it and because it's so dynamic, I don't think it works with table functions (and thus must be a SP). The downside to a SP is that I can't join to it. And again, thanks!

– pdalbe01 Oct 21 at 15:30 Welcome, sorry for didn't testing it on SQL2005, I amended it – Adrian Iftode Oct 21 at 15:58 You can't use it in a table function, since "In inline table-valued functions, the TABLE return value is defined through a single SELECT statement. " msdn.microsoft.Com/en-us/library/ms186755. Aspx – Adrian Iftode Oct 21 at 16:11 However, you said that the Attributes are not so volatile, so you can use the generated statement in a table function – Adrian Iftode Oct 21 at 16:22.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions