Dynamic Pivot Needed with Row_Number()

人盡茶涼 提交于 2020-07-13 01:24:19

问题


I am using Microsoft SQL Server Management Studio 2008.

I have data that looks like this:

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

and I'd like the results to appear like this:

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         

The columns are unknown so I know I need a dynamic query. I have tried a dynamic query with a pivot function but only got groupings under the same type of value. So the aggregate function worked against me.

This was the query I tried:

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;'

Exec (@DynamicPivotQuery)

Next I added the row_number and partition functions but can't seem to debug this. The error I get is:

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

Which is near the XML Path function.

Any help on this would be appreciated. Thanks.

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 

回答1:


with using a cte, with row_number you can achieve the result:

Your Schema:

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');

The query:

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

The Result:

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


来源:https://stackoverflow.com/questions/29569801/dynamic-pivot-needed-with-row-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!