복붙노트

[SQL] ROW_NUMBER에 필요한 동적 피벗 ()

SQL

ROW_NUMBER에 필요한 동적 피벗 ()

나는 마이크로 소프트 SQL Server 관리 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와 파티션 기능을 추가하지만,이를 디버깅 할 수없는 것. 오류 I get 및입니다 :

어느 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 

해결법

  1. ==============================

    1.ROW_NUMBER와 더불어, CTE를 사용하여 당신은 결과를 얻을 수 있습니다 :

    ROW_NUMBER와 더불어, CTE를 사용하여 당신은 결과를 얻을 수 있습니다 :

    스키마 :

    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
    
  2. from https://stackoverflow.com/questions/29569801/dynamic-pivot-needed-with-row-number by cc-by-sa and MIT license