1.這是源數據以及需要轉化的目標表.
我們的方法是 , 用 GROUP BY按照year分組 , 並且依次提取1月,2月,3月,4月的 num,具體實現
select year, max(case when month=1 then money else 0 end) as M1, max(case when month=2 then money else 0 end) as M2, max(case when month=3 then money else 0 end) as M3, max(case when month=4 then money else 0 end) as M4 from sale group by year;
其中select year是選擇年 , 配合后面GROUP BY
中間的max是獲取其中等於匹配到的值, 為什么要寫個0 , 其實是為了去重 , 也是為了部位null