2017-09-20 42 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