2013-12-17 1 views
0

일부 행을 피벗하려는 테이블이 있습니다. 여기에이 질문들이 많이 있지만, 아직도 고민 중입니다.집계가없는 피벗 - 다시

다음은 내가 시작한 표입니다.

select userlicenseid, 
    startdate, 
    max(case when name = 'Other' then value end) Other, 
    max(case when name = 'Pathways' then value end) Pathways, 
    max(case when name = 'Execution' then value end) Execution, 
    max(case when name = 'Focus' then value end) Focus, 
    max(case when name = 'Profit' then value end) Profit 
from yourtable 
group by userlicenseid, startdate; 

SQL Fiddle with Demo를 참조하십시오 : 나는 당신의 샘플 데이터를 기반으로 Ending Point

답변

4

에 도착 할 위치 여기 Starting Point

쉽게 CASE 표현식으로 집계 함수를 사용하여 결과를 얻을 수있다 . 문자열 값을 열로 변환하기 때문에 min() 또는 max() 집합체를 사용해야합니다.

당신은뿐만 아니라 결과를 얻을 수 PIVOT 기능을 사용할 수

select userlicenseid, startdate, 
    Other, Pathways, Execution, Focus, Profit 
from 
(
    select userlicenseid, startdate, 
    name, value 
    from yourtable 
) d 
pivot 
(
    max(value) 
    for name in (Other, Pathways, Execution, Focus, Profit) 
) piv; 

참조 SQL Fiddle with Demo