Row_Number() 需要动态枢轴
Posted
技术标签:
【中文标题】Row_Number() 需要动态枢轴【英文标题】:Dynamic Pivot Needed with Row_Number() 【发布时间】:2015-04-10 20:02:25 【问题描述】:我正在使用 Microsoft SQL Server Management Studio 2008。
我的数据如下所示:
Client ID Value
-------------------------------
12345 Did Not Meet
12345 Did Not Meet
12345 Partially Met
12346 Partially Met
12346 Partially Met
12346 Partially Met
12347 Partially Met
12347 Partially Met
12347 Did Not Meet
12347 Met
我希望结果如下所示:
Client ID Value1 Value2 Value3 Value4
12345 Did Not Meet Did Not Meet Partially Met NULL
12346 Partially Met Partially Met Partially Met NULL
12347 Partially Met Partially Met Did Not Meet Met
这些列是未知的,所以我知道我需要一个动态查询。我尝试了一个带有数据透视函数的动态查询,但只得到了相同类型的值下的分组。所以聚合函数对我不利。
这是我尝试过的查询:
Declare @Columns nvarchar(max);
Declare @DynamicPivotQuery nvarchar(max);
Select @Columns=
COALESCE(@Columns+',','')+QUOTENAME(Value)
from (select distinct Document.Value
from Document d
join Client c on d.clientid=c.id
)
as t1
Set @DynamicPivotQuery=
N'Select ClientID, ' + @Columns + '
from
(select Document.ClientID,
DocumentFact.Value,
from Document d
join Client c on d.clientid=c.id
) p
Pivot (max(Value) for Value in ('+@Columns+'))
as pivottable
order by ClientID;'
执行 (@DynamicPivotQuery)
接下来我添加了 row_number 和 partition 函数,但似乎无法调试它。我得到的错误是:
消息 102,第 15 级,状态 1,第 29 行 ')' 附近的语法不正确。
在 XML Path 函数附近。
对此的任何帮助将不胜感激。谢谢。
select @Columns=
COALESCE(@Columns+',','')+QUOTENAME(Value)
from (select distinct Document.Value
, 'name'+ CAST (row_number() over
(Partition BY clientid order by clientid) as NVARCHAR (10)) as Cols
from document d
join Clients c on d.clientid=c.id
t1
--FOR XML PATH('')), 1, 1, N'');
FOR XML PATH('')), TYPE).value('.','NVARCHAR(MAX)'),1,2,'')
order by ClientID
【问题讨论】:
你不能在脚本中做吗,比如 php、python、javascript? @WixLove 请随时对below answer提供反馈 【参考方案1】:使用cte
,使用row_number
,您可以获得结果:
您的架构:
create table your_table([Client ID] int ,Value varchar(50));
insert into your_table values
(12345, 'Did Not Meet'),
(12345, 'Did Not Meet'),
(12345, 'Partially Met'),
(12346, 'Partially Met'),
(12346, 'Partially Met'),
(12346, 'Partially Met'),
(12347, 'Partially Met'),
(12347, 'Partially Met'),
(12347, 'Did Not Meet'),
(12347, 'Met');
查询:
with cte as
(
select [Client ID] ci,value,
row_number() over(partition by [Client ID] order by value) as rn
from your_table
)
select distinct ci as [Client ID],
(select ct.value from cte ct where ct.ci=cte.ci and ct.rn=1) value1,
(select ct.value from cte ct where ct.ci=cte.ci and ct.rn=2) value2,
(select ct.value from cte ct where ct.ci=cte.ci and ct.rn=3) value3,
(select ct.value from cte ct where ct.ci=cte.ci and ct.rn=4) value4
from cte
结果:
Client ID value1 value2 value3 value4
12345 Did Not Meet Did Not Meet Partially Met (null)
12346 Partially Met Partially Met Partially Met (null)
12347 Did Not Meet Met Partially Met Partially Met
【讨论】:
抱歉——我正在重写我的评论,因为第一次并不清楚。感谢您的答复。我试图在 SQL Fiddle 上运行您的示例,但根本无法运行您的查询。上面的帖子是我查询的简化版本。我已经自定义了更复杂的查询,但没有收到错误消息,但查询运行时间超过 15 分钟。我不知道为什么我不能让你的查询在 SQL Fiddle 中运行。 我终于能够通过添加一些过滤器来使您建议的查询工作,这些过滤器使运行时间更快,因此我可以看到结果。这正是我想要的,非常感谢! 不客气,兄弟,在cte
之后的select
部分,您也可以使用4 个self joins
代替子查询。以上是关于Row_Number() 需要动态枢轴的主要内容,如果未能解决你的问题,请参考以下文章