• 行列转换


    SELECT 列 ,MAX(CASE 列名1 WHEN '某个值' THEN 列名2 ELSE 0 END )别名 FROM 表名 GROUP BY 列;

    列子:

    CREATE TABLE TabName (
    Id int(11) NOT NULL,
    Name varchar(20) DEFAULT NULL,
    Date date DEFAULT NULL,
    Scount int(11) DEFAULT NULL
    );

    INSERT INTO `TabName` VALUES ('1', '小说', '2013-09-01', '10000');
    INSERT INTO `TabName` VALUES ('2', '微信', '2013-09-01', '20000');
    INSERT INTO `TabName` VALUES ('3', '小说', '2013-09-02', '30000');
    INSERT INTO `TabName` VALUES ('4', '微信', '2013-09-02', '35000');
    INSERT INTO `TabName` VALUES ('5', '小说', '2013-09-03', '31000');
    INSERT INTO `TabName` VALUES ('6', '微信', '2013-09-03', '36000');
    INSERT INTO `TabName` VALUES ('7', '小说', '2013-09-04', '35000');
    INSERT INTO `TabName` VALUES ('8', '微信', '2013-09-04', '38000');

    SELECT * FROM `TabName`;

    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;

  • 相关阅读:
    ztree学习---将默认勾选的展开
    CentOS之RPM
    CentOS之文档的压缩与打包
    CentOS之Vim
    CentOS用户和用户组管理
    Linux CentOS更改文件的权限
    CentOS的文件属性:命令 ls -l
    CentOS命令
    Java解析excel
    easyUI的combotree的树的懒加载。
  • 原文地址:https://www.cnblogs.com/colos/p/9961699.html
Copyright © 2020-2023  润新知