sql-行列转换
Posted jentary
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql-行列转换相关的知识,希望对你有一定的参考价值。
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、查看数据
- SELECT * FROM tabName ;
2、列转行统计数据
- 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
- SELECT DATE,GROUP_CONCAT(NAME) FROM tabname GROUP BY DATE
4、行转列统计数据
- SELECT DATE, GROUP_CONCAT(NAME,‘总量:‘,Scount) AS b_str FROM tabName GROUP BY DATE
- SELECT DATE,NAME, GROUP_CONCAT(NAME,‘总量:‘,Scount) AS b_str FROM TabName GROUP BY DATE ,NAME
以上是关于sql-行列转换的主要内容,如果未能解决你的问题,请参考以下文章