我已經在MySQL下表換位MySQL的行與數據定製列
emp_id month salary
1 Jan 2000
1 Feb 2000
1 March 2000
2 Jan 3000
2 Feb 3000
2 March 3000
我試圖得到一個結果類似以下內容:
emp_id Jan_sal feb_sal Mar_sal
1 2000 2000 2000
2 3000 3000 3000
我的查詢:
select emp_id,
(case when month = 'Jan' then salary else NUll end) jan_sal,
(case when month = 'Feb' then salary else NUll end) feb_sal,
(case when month = 'March' then salary else NUll end) march_sal
from emp group by emp_id;
但我得到的輸出爲:
emp_id jan_sal feb_sal march_sal
1 2000 NULL NULL
2 3000 NULL NULL
不知道我的查詢是否正確。 任何幫助表示讚賞。
總和(),MAX()等 - 但我會處理在表示層 – Strawberry