SELECT a.`name`,a.mon,
SUM(CASE RIGHT(a.mon,2) WHEN ‘01‘ THEN sa ELSE 0 END) ‘01月‘,
SUM(CASE RIGHT(a.mon,2) WHEN ‘02‘ THEN sa ELSE 0 END) ‘02月‘,
SUM(CASE RIGHT(a.mon,2) WHEN ‘03‘ THEN sa ELSE 0 END) ‘03月‘,
SUM(CASE RIGHT(a.mon,2) WHEN ‘04‘ THEN sa ELSE 0 END) ‘04月‘,
SUM(CASE RIGHT(a.mon,2) WHEN ‘05‘ THEN sa ELSE 0 END) ‘05月‘,
SUM(CASE RIGHT(a.mon,2) WHEN ‘06‘ THEN sa ELSE 0 END) ‘06月‘,
SUM(CASE RIGHT(a.mon,2) WHEN ‘07‘ THEN sa ELSE 0 END) ‘07月‘,
SUM(CASE RIGHT(a.mon,2) WHEN ‘08‘ THEN sa ELSE 0 END) ‘08月‘,
SUM(CASE RIGHT(a.mon,2) WHEN ‘09‘ THEN sa ELSE 0 END) ‘09月‘,
SUM(CASE RIGHT(a.mon,2) WHEN ‘10‘ THEN sa ELSE 0 END) ‘10月‘,
SUM(CASE RIGHT(a.mon,2) WHEN ‘11‘ THEN sa ELSE 0 END) ‘11月‘,
SUM(CASE RIGHT(a.mon,2) WHEN ‘12‘ THEN sa ELSE 0 END) ‘12月‘
FROM sale a
GROUP BY a.mon
行转列列传行更具体例子
1、查看数据
Sql代码
- SELECT * FROM tabName ;
2、列转行统计数据
Sql代码
- SELECT DATE ,
- MAX(CASE NAME WHEN ‘小说‘ THEN Scount ELSE 0 END ) 小说,
- MAX(CASE NAME WHEN ‘微信‘ THEN Scount ELSE 0 END ) 微信
- FROM TabName
- GROUP BY DATE
3、行转列
Sql代码
- SELECT DATE,GROUP_CONCAT(NAME) FROM tabname GROUP BY DATE
4、行转列统计数据
Sql代码
- SELECT DATE, GROUP_CONCAT(NAME,‘总量:‘,Scount) AS b_str FROM tabName GROUP BY DATE
Sql代码
- SELECT DATE,NAME, GROUP_CONCAT(NAME,‘总量:‘,Scount) AS b_str FROM TabName GROUP BY DATE ,NAME
原文地址:https://www.cnblogs.com/jentary/p/11579319.html