• sql 统计用的sql


    mh:工时   mhtype:工时类型(6种)

    字段:userid      mhtype    mh
          001          1        5
          001          1        3
          001          2        4
          001          3        9
          002          5        5
          002          6        7
          002          3        4
          002          3        9

    要求统计出每个人每一类工时的总合
    结果要求如下格式
    userid  mhtype1   mhtype2   mhtype3   mhtype4   mhtype5   mhtype6   allmh
    001     8         4          9        0           0         0        21
    002     0         0          13        0           5         7        25

    -------------------

    create table lk4(
    userid mediumint(3) unsigned zerofill not null,
    mhtype int not null,
    mh int not null);
    insert into lk4 values
    (001,1,5),
    (001,1,3),
    (001,2,4),
    (001,3,9),
    (002,5,5),
    (002,6,7),
    (002,3,4),
    (002,3,9);
     
    select userid,
    sum((case when mhtype=1 then mh else end )) as 'mhtype1',
    sum((case when mhtype=2 then mh else end )) as 'mhtype2',
    sum((case when mhtype=3 then mh else end )) as 'mhtype3',
    sum((case when mhtype=4 then mh else end )) as 'mhtype4',
    sum((case when mhtype=5 then mh else end )) as 'mhtype5',
    sum((case when mhtype=6 then mh else end )) as 'mhtype6',
    sum(mh) as 'allmh'
    from lk4 group by userid;



    结果:

    +--------+---------+---------+---------+---------+---------+---------+-------+
    | userid | mhtype1 | mhtype2 | mhtype3 | mhtype4 | mhtype5 | mhtype6 | allmh |
    +--------+---------+---------+---------+---------+---------+---------+-------+
    |    001 |       8 |       4 |       9 |       0 |       0 |       0 |    21 |
    |    002 |       0 |       0 |      13 |       0 |       5 |       7 |    25 |
    +--------+---------+---------+---------+---------+---------+---------+-------+
    2 rows in set (0.00 sec)

    *******************************

    数据结构如下
     id    name    area
     01    a       河北
     02    b       山东
     03    c       河北  
     04    d       陕西
     05    e       河北
     06    f       山东
     
    也即每个用户都有id,name,area
     现要求如下:
     按地区聚合并统计用户数、显示各用户详细情况
     
    大概类似于下面的结果:
     地区  总用户数   详细用户
     河北   3       (01,a),(03,c),(05,e)
     山东   2       (02,b),(06,f)
     陕西   1       (04,d)

    select area,count(*),group_concat('(',id,',',name,')')
    from tb
    group by area

    ************

     

    这是源数据,根据不同的adminid,算出每个adminid有多少条数据,并且state为0的有几条,为1的有几条
    返回结果是这样的:

    adminid total state0 state1
       1         100   90        10
      2          111   55        56
    total是state的总数,state0是state=0的数量,state1是state=1的数量,adminid是adminid


    然后将以下得到的数据全部插入表2内:

    表2字段为:

    adminid s_total s_state0 s_state1

    insert into tb2
    select adminid,count(*) as total,
    count(case when state=0 then 1 end) as state0,
    count(case when state=1 then 1 end) as state1
    from tb
    group by adminid

    ***********

     一。以产品编码为分类进行数量的汇总

    SQL code?
    1
    2
    3
    select 产品编码,sum(购买数量)
    from 表结构
    group by 产品编码



    二。在汇总的基础上,我要对每个产品编码按发货地区进行分类汇总。

    SQL code?

    1
    2
    3
    select 产品编码,发货地区,sum(购买数量)
    from 表结构
    group by 产品编码,发货地区
  • 相关阅读:
    MySQL错误码
    环境小硕的转行之路-25-模块的导入与管理
    环境小硕的转行之路-24-大作业:编写计算机程序
    环境小硕的转行之路-23-异常处理
    环境小硕的转行之路-22-os,pickle的作业
    环境小硕的转行之路-21-序列化
    环境小硕的转化之路-20-random,time,sys,os模块
    环境小硕的转行之路-19- re模块进阶
    环境小硕的转行之路-18-模块以及正则表达式
    环境小硕的转行之路-16-生成器函数、推导式、生成器表达式
  • 原文地址:https://www.cnblogs.com/zeroone/p/3226456.html
Copyright © 2020-2023  润新知