• 行列转换


    https://blog.csdn.net/mchdba/article/details/39163695

    第一步,求两个表各自的数据
    select DATE(a.create_date) as 'nianfen',
    sum(IF(a.type="0",1,0)) AS "shanghu",
    SUM(IF(a.type="1",1,0)) AS "banzu",
    SUM(IF(a.type="2",1,0)) AS "company",
    0 as 'zhaogong',
    0 as 'zhaohuo',
    0 as 'gongqiu',
    0 as 'gongcheng',
    0 as 'jianshe',
    0 as 'tujian',
    0 as 'advert',
    0 as 'dianqi',
    0 as 'yuanlin'
    from
    dp_info a where DATE_FORMAT(create_date,'%Y-%m')='2018-05'
    group by DATE(a.create_date)

    union all

    select DATE(a.date) as 'nianfen',
    0 as 'shanghu',
    0 as 'banzu',
    0 as 'company',
    sum(IF(a.to_type="招工信息",1,0)) AS "zhaogong",
    SUM(IF(a.to_type="找活信息",1,0)) AS "zhaohuo",
    SUM(IF(a.to_type="供求信息",1,0)) AS "gongqiu",
    SUM(IF(a.to_type="工程机械",1,0)) AS "gongcheng",
    SUM(IF(a.to_type="建设服务",1,0)) AS "jianshe",
    SUM(IF(a.to_type="土建材料",1,0)) AS "tujian",
    SUM(IF(a.to_type="装修材料",1,0)) AS "advert",
    SUM(IF(a.to_type="电气材料",1,0)) AS "dianqi",
    SUM(IF(a.to_type="园林景观",1,0)) AS "yuanlin"
    from
    fb_fabu a where DATE_FORMAT(date,'%Y-%m')='2018-05'
    group by DATE(a.date)


    第二步,求两个表合在一起的数据
    select
    xx.nianfen as 'nianfen',
    sum(xx.zhaogong) as "zhaogong",
    sum(xx.zhaohuo) as 'zhaohuo',
    sum(xx.gongqiu) as 'gongqiu',
    sum(xx.gongcheng) as 'gongcheng',
    sum(xx.jianshe) as 'jianshe',
    sum(xx.shanghu) as 'shanghu',
    sum(xx.company) as 'company',
    sum(xx.banzu) as 'banzu',
    SUM(xx.tujian) AS "tujian",
    SUM(xx.advert) AS "advert",
    SUM(xx.dianqi) AS "dianqi",
    SUM(xx.yuanlin) AS "yuanlin",
    SUM(xx.tujian) AS "tujian"
    SUM(IF(a.to_type="园林景观",1,0)) AS "yuanlin"
    from
    (
    第一步内容
    ) xx
    GROUP BY xx.nianfen


    第三步,求和
    select
    '总计' as 'nianfen',
    sum(ddd.zhaogong) as "zhaogong",
    sum(ddd.zhaohuo) as 'zhaohuo',
    sum(ddd.gongqiu) as 'gongqiu',
    sum(ddd.gongcheng) as 'gongcheng',
    sum(ddd.jianshe) as 'jianshe',
    sum(ddd.shanghu) as 'shanghu',
    sum(ddd.company) as 'company',
    sum(ddd.banzu) as 'banzu',
    SUM(ddd.tujian) AS "tujian",
    SUM(ddd.advert) AS "advert",
    SUM(ddd.dianqi) AS "dianqi",
    SUM(ddd.yuanlin) AS "yuanlin"
    from
    (
    第二步的内容
    )ddd

    第四部合并

  • 相关阅读:
    sudo rm /var/cache/apt/archives/lock sudo rm /var/lib/dpkg/lock
    redis常用命令大全
    MySQL explain使用
    rabbitmq添加user及vhost
    Make sure you've included captcha.urls as explained in the INSTALLATION section on
    django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applie
    ARM实验5 —— 按键中断实验
    ARM实验4—按键轮询实验
    ARM实验3 ——串口实验
    ARM实验2 —— 蜂鸣器实验
  • 原文地址:https://www.cnblogs.com/gtbky/p/9072917.html
Copyright © 2020-2023  润新知