Row_Number() 需要动态枢轴

Dynamic Pivot Needed with Row_Number()

我正在使用 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 和分区函数,但似乎无法对其进行调试。我得到的错误是:

Msg 102, Level 15, State 1, Line 29
Incorrect syntax near ')'.

在 XML 路径功能附近。

如有任何帮助,我们将不胜感激。谢谢。

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 

使用 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