我正在尝试pivot without aggregation,并且碰到了一堵墙。
以下是我试图开始工作的示例T-SQL:
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给了我2个错误:
- Msg 8114,Level 16,State 1,Line 4 将数据类型nvarchar转换为bigint时出错。
- Msg 473,Level 16,State 1,Line 4 PIVOT运算符中提供了错误的值“Field1”。
“debug”select语句返回:
这篇文章(上面的链接)似乎很有希望,但我似乎无法让它发挥作用。我究竟做错了什么?或者这篇文章是完全错误的,我想要做的是不可能的?
我已经看到了许多类似的SO问题,但要么它们涉及可以“正常工作”聚合的全数字字段,要么它们涉及可以实现为简单连接的已知列 - 我不知道是什么我将要选择OptionalFieldName
个值,OptionalFieldValue
值是无法聚合的字符串,至少是AFAIK。
答案 0 :(得分:5)
我对你为什么试图用row_number()
欺骗这一点感到有些困惑。即使您有字符串值,您仍然可以对其进行汇总 - 您只需使用max
或min
即可获得结果。
我总是建议首先尝试使用硬编码值编写查询,尤其是在尝试使用动态SQL之前使用PIVOT时。我不确定为什么你不能这样写这样的查询:
select Id, Name, Field1, Field2, Field3
from
(
select
Id
,Name
,OptionalFieldName
,OptionalFieldValue
from #tmp
) d
pivot
(
max(OptionalFieldValue)
for OptionalFieldName in (Field1, Field2, Field3)
) piv;
然后,如果你真的需要动态SQL,你只需要编写它:
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);
set @cols = stuff((select distinct ',' + quotename(OptionalFieldName)
from @joined
for xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, '');
select * into #tmp from @joined
DECLARE @query AS NVARCHAR(MAX)
set @query = 'SELECT Id, Name,' + @cols + '
from
(
select Id
,Name
,OptionalFieldName
,OptionalFieldValue
from #tmp
) x
pivot
(
max(OptionalFieldValue)
for OptionalFieldName in (' + @cols + ')
) p '
execute(@query);
See Demo。两个版本似乎都给出了您所请求的结果。
答案 1 :(得分:0)
希望我明白你的需要:
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');
select
data.Id
,data.Name
,opt.Name as Name1
,opt.Value into #tmp
from @data data
inner join @optionalFields opt on data.Id = opt.ParentId
declare @cols as nvarchar(max) =
stuff((select distinct ',' + quotename(Name1) from #tmp for xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, '');
DECLARE @cols1 as nvarchar(max) =
stuff((select distinct +',MAX(CASE WHEN (pvt1.'+quotename(Name1) +' = ros.RN AND pvt1.id = ros.id) THEN ros.Value ELSE NULL END) as '+quotename(Name1) from #tmp for xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, '');
declare @query as nvarchar(max) = '
;WITH cte AS(
SELECT * FROM #tmp
),
ros AS (
SELECT ROW_NUMBER() OVER (partition by Id order by Id) AS [RN],id,Value
FROM cte),
pvt1 AS (
select *
from (select Id, Name, Name1, row_number() over (partition by Id order by Id) RN
from cte) src
pivot (max(RN) for Name1 in ('+@cols+')) pvt)
SELECT pvt1.ID,
pvt1.Name,
'+@cols1+'
FROM pvt1
CROSS JOIN ros
GROUP BY pvt1.ID,
pvt1.Name'
execute(@query);
drop table #tmp
结果:
ID Name Field1 Field2 Field3
1 Test record 1 Foo Bar 42
2 Test record 2 Bar Foo 24
如果您添加更多字段,例如insert into @optionalFields values (2, 'Field4', '15');
,您将获得:
ID Name Field1 Field2 Field3 Field4
1 Test record 1 Foo Bar 42 NULL
2 Test record 2 Bar Foo 24 15