2017-09-20 44 views
1

我有一个包含多个作业和多个部门的表,我希望以这样的方式处理数据,以便它能够提供部门明智和工作明智的薪资信息。我怎样才能做到这一点?只能使用select和case还是类似来实现?在MySql中生成伪列

+0

提醒一下,如果有人为您提供了可行的答案,请点击答案旁边的绿色复选标记,将其标记为“已接受”答案。 –

+0

Upvoting,因为Q和A是“枢轴表”中最简洁的部分。 –

回答

4
select deptno, 
     sum(case when job = 'Founder' then salary else 0 end) as Founder, 
     sum(case when job = 'Cofounder' then salary else 0 end) as Cofounder, 
     sum(case when job = 'Sales' then salary else 0 end) as Sales, 
     sum(case when job = 'Marketing' then salary else 0 end) as Marketing, 
     sum(case when job = 'Janitor' then salary else 0 end) as Janitor 
from your_table 
group by deptno