SQL Pivot。需要以下格式输出

时间:2016-08-13 18:55:26

标签: sql

下表是temp_v1的详细信息。

HOURS      NAME     SALARY DATE 
3          JOHN     60     14-Jul-16    
5          JOHNSON  10     14-Jul-16    
34         JOHNSON  70     16-Jul-16    
45         JOHNSON  90     18-Jul-16    

我希望输出为-------------->

DATE        JOHN_Hrs John_sal JOHNSON_Hrs JOHNSON_sal
14-Jul-16   3        60       5           10
16-Jul-16   0        0        34          70
18-Jul-16   0        0        45          90

有人可以帮我做这个。

1 个答案:

答案 0 :(得分:1)

以下是使用pivot {/ 1}} conditional aggregation结果的通用方法。

select salary_date,
       max(case when name = 'john' then hours else 0 end) john_hours,
       max(case when name = 'john' then salary else 0 end) john_sal,
       max(case when name = 'johnson' then hours else 0 end) johnson_hours,
       max(case when name = 'johnson' then salary else 0 end) johnson_sal
from yourtable
group by salary_date