2013-12-17 47 views
0

我有一張表,我想要旋轉一些行。這裏有很多這樣的問題,但我仍然在掙扎。沒有集合的樞軸 - 再次

這是我從一開始的表格。 Starting Point

這裏就是我想根據您的樣本數據去 Ending Point

回答

4

,你可以很容易地使用帶有CASE表達式中的聚合函數得到的結果:

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 。由於您將字符串值轉換爲列,因此您將要使用min()max()聚合。

您可以使用旋轉功能得到結果,以及:

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