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 产品编码,发货地区

sql 统计用的sql的更多相关文章

  1. sql 统计常用的sql

    统计常用的sql  统计常用的sql语句: 今天的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())=0 昨天的所有数据:s ...

  2. MS SQL 统计信息浅析上篇

    统计信息概念 统计信息是一些对象,这些对象包含在表或索引视图中一列或多列中的数据分布有关的统计信息.数据库查询优化器使用这些统计信息来估计查询结果中的基数或行数. 通过这些基数估计,查询优化器可以生成 ...

  3. MS SQL统计信息浅析下篇

       MS SQL统计信息浅析上篇对SQL SERVER 数据库统计信息做了一个整体的介绍,随着我对数据库统计信息的不断认识.理解,于是有了MS SQL统计信息浅析下篇. 下面是我对SQL Serve ...

  4. SQL统计——按照各种维度

    在SQLserver中可以按照各种维度进行统计,实现与EXCLE一样强大的功能. --========================== --Blog:<奔跑的金鱼> --Desc:&l ...

  5. sql统计字符串出现次数技巧

    在牛客网上看到一道题,感觉挺有趣,是用sql统计字符串出现的次数. 这里提供一种思路,比如统计字符串A中子串B的出现次数: SELECT (LENGTH(A) - LENGTH(REPLACE(A, ...

  6. ORACLE 统计文件后缀SQL

    ORACLE 统计文件后缀SQL查询语句 select lower(substr(file_name,instr(file_name, '.', -1),length(file_name))),cou ...

  7. SQL Server中的SQL语句优化与效率问题

    很多人不知道SQL语句在SQL SERVER中是如何执行的,他们担心自己所写的SQL语句会被SQL SERVER误解.比如: select * from table1 where name='zhan ...

  8. SQL Server Profiler监控SQL Server性能

    全面掌握SQL Server Profiler 1.       原理与相关概念介绍 SQL Server Profiler,大家已经非常熟悉.常常在性能优化中使用,本文档详细介绍SQL Server ...

  9. 整理:sql语句优化之SQL Server

    . 增加服务器CPU个数;但是必须明白并行处理串行处理更需要资源例如内存.使用并行还是串行程是MsSQL自动评估选择的.单个任务分解成多个任务,就可 以在处理器上运行.例如耽搁查询的排序.连接.扫描和 ...

随机推荐

  1. poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)

    http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  2. replace()替换文字扑获组做法

    var txt = "12312131283", str = txt.replace(/(12(.3))/g,"中文$2");//$1是针对前面的扑获组()的如 ...

  3. uva 10154

    dp  记忆化搜索 做的时候像dfs  #include <iostream> #include <cstring> #include <cstdio> #incl ...

  4. 如何定位Release 版本中程序崩溃的位置 ---利用map文件 拦截windows崩溃函数

    1       案例描述 作为Windows程序员,平时最担心见到的事情可能就是程序发生了崩溃(异常),这时Windows会提示该程序执行了非法操作,即将关闭.请与您的供应商联系.呵呵,这句微软的“名 ...

  5. 用MT.exe将exe中的manifest文件提取出来和将manifest文件放入exe中

     前一种方法是将manifest文件放入exe中,但是要记得需要在工程中设置 这样的话exe中就不存在manifest了,在debug目录下就会看到相应的manifest文件.后者是将exe中的man ...

  6. vc2008程序发布指南

    vc2008程序发布指南 2008-05-03 17:46 vc2008开发的程序的发布方式可以有5种方式: 1. 采用静态链接到crt和MFC. 只要你拥有组成程序的所有源代码,你就可以采用这种方式 ...

  7. 【leetcode】Palindrome Number (easy)

    Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...

  8. hdu 4336 Card Collector 容斥原理

    读完题目就知道要使用容斥原理做! 下面用的是二进制实现的容斥原理,详见:http://www.cnblogs.com/xin-hua/p/3213050.html 代码如下: #include< ...

  9. MySQL在windows和linux下的表名大小写问题

    MySQL在windows下是不区分大小写的,将script文件导入MySQL后表名也会自动转化为小写,结果再想要将数据库导出放到linux服务 器中使用时就出错了.因为在linux下表名区分大小写而 ...

  10. [topcoder]BadNeighbors

    http://community.topcoder.com/stat?c=problem_statement&pm=2402&rd=5009 动态规划题.对于圈状的题目有了点感觉. 题 ...