I'm trying to pivot without aggregation, and running into a bit of a wall.
Here's the sample T-SQL I'm trying to get to work:
declare @optionalFields table (ParentId int, Name nvarchar(50), Value nvarchar(50));
insert into @optionalFields values (1, 'Field1', 'Foo');
insert into @optionalFields values (1, 'Field2', 'Bar');
insert into @optionalFields values (1, 'Field3', '42');
insert into @optionalFields values (2, 'Field1', 'Bar');
insert into @optionalFields values (2, 'Field2', 'Foo');
insert into @optionalFields values (2, 'Field3', '24');
declare @data table (Id int, Name nvarchar(50));
insert into @data values (1, 'Test record 1');
insert into @data values (2, 'Test record 2');
declare @joined table (Id int, Name nvarchar(50), OptionalFieldName nvarchar(50), OptionalFieldValue nvarchar(50));
insert into @joined
select
data.Id
,data.Name
,opt.Name
,opt.Value
from @data data
inner join @optionalFields opt on data.Id = opt.ParentId
declare @cols as nvarchar(max) =
stuff((select distinct ',' + quotename(OptionalFieldName) from @joined for xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, '');
select * into #tmp from @joined
-- just to see that it's returning the expected values (it does)
select
Id
,Name
,OptionalFieldName
,OptionalFieldValue
,row_number() over (partition by Id order by Id) RN
from #tmp -- this is the FROM clause in the below dynamic-sql query
declare @query as nvarchar(max) = '
select Id, Name, ' + @cols + '
from (select Id, Name, OptionalFieldName, OptionalFieldValue, row_number() over (partition by Id order by Id) RN from #tmp) src
pivot (max(OptionalFieldName) for RN in (' + @cols + ')) pvt';
execute(@query);
drop table #tmp;
SSMS is giving me 2 errors:
- Msg 8114, Level 16, State 1, Line 4 Error converting data type nvarchar to bigint.
- Msg 473, Level 16, State 1, Line 4 The incorrect value "Field1" is supplied in the PIVOT operator.
The "debug" select statement is returning this:
The article (link above) seemed very promising, however I can't seem to be able to get it to work. What am I doing wrong? Or is this article outright wrong and what I'm trying to do is impossble?
I've seen a number of similar SO questions, but either they involved all-numeric fields that could "just work" with aggregation, or they involved known columns that could be implemented as simple joins - I don't know what OptionalFieldName
values I'm going to be selecting, and the OptionalFieldValue
values are strings that simply can't be aggregated, at least AFAIK.
OptionalFieldValue
cannot be aggregated as it's a varchar... pivoting on RN is used to trick the pivoting, as shown in the article I linked to.', MAX(' + quotename(OptionalFieldName) + ') ' + quotename(OptionalFieldName)
then change your @query to:declare @query as nvarchar(max) = ' select Id, Name, ' + @cols2 + ' from #tmp pivot (max(OptionalFieldValue) for OptionalFieldName in (' + @cols + ')) pvt GROUP BY ID, Name';
min
/max
on it. Here is a demo - sqlfiddle.com/#!3/9eecb7/7786/0 That can be easily converted to dynamic sql (sqlfiddle.com/#!3/9eecb7/7790/0).