zoukankan      html  css  js  c++  java
  • 行列转换

    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

    第四部合并

  • 相关阅读:
    强迫症患者
    GG的匹配串
    漂洋过海来看你
    Fire or Retreat
    1011. A+B和C (15)
    1010. 一元多项式求导 (25)
    1009. 说反话 (20)
    1008. 数组元素循环右移问题 (20)
    1007. 素数对猜想 (20)
    1006. 换个格式输出整数 (15)
  • 原文地址:https://www.cnblogs.com/gtbky/p/9072917.html
Copyright © 2011-2022 走看看