数据库SQL代码笔记_行转列
-- MySQL行转列 select year, -- 年份 max(case month when 1 then amount else 0 end) M1, -- 一月 max(case month when 2 then amount else 0 end) M2, -- 二月 max(case month when 3 then amount else 0 end) M3, -- 三月 max(case month when 4 then amount else 0 end) M4 -- 四月 from calendar group by year;
-- 原表 id year month amount 0 2018 1 20 1 2018 2 30 2 2018 3 50 3 2018 4 60 4 2019 1 60 -- 行表转列表 year M1 M2 M3 M4 2018 20 30 50 60 2019 60 0 0 0
数据库SQL代码笔记_行转列
最新推荐文章于 2023-03-10 16:48:03 发布