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
0
end
))
as
'mhtype1'
,
sum
((
case
when
mhtype=2
then
mh
else
0
end
))
as
'mhtype2'
,
sum
((
case
when
mhtype=3
then
mh
else
0
end
))
as
'mhtype3'
,
sum
((
case
when
mhtype=4
then
mh
else
0
end
))
as
'mhtype4'
,
sum
((
case
when
mhtype=5
then
mh
else
0
end
))
as
'mhtype5'
,
sum
((
case
when
mhtype=6
then
mh
else
0
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
***********
一。以产品编码为分类进行数量的汇总
1
2
3
|
select 产品编码, sum (购买数量) from 表结构 group by 产品编码 |
二。在汇总的基础上,我要对每个产品编码按发货地区进行分类汇总。
1
2
3
|
select 产品编码,发货地区, sum (购买数量) from 表结构 group by 产品编码,发货地区 |
sql 统计用的sql的更多相关文章
- sql 统计常用的sql
统计常用的sql 统计常用的sql语句: 今天的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())=0 昨天的所有数据:s ...
- MS SQL 统计信息浅析上篇
统计信息概念 统计信息是一些对象,这些对象包含在表或索引视图中一列或多列中的数据分布有关的统计信息.数据库查询优化器使用这些统计信息来估计查询结果中的基数或行数. 通过这些基数估计,查询优化器可以生成 ...
- MS SQL统计信息浅析下篇
MS SQL统计信息浅析上篇对SQL SERVER 数据库统计信息做了一个整体的介绍,随着我对数据库统计信息的不断认识.理解,于是有了MS SQL统计信息浅析下篇. 下面是我对SQL Serve ...
- SQL统计——按照各种维度
在SQLserver中可以按照各种维度进行统计,实现与EXCLE一样强大的功能. --========================== --Blog:<奔跑的金鱼> --Desc:&l ...
- sql统计字符串出现次数技巧
在牛客网上看到一道题,感觉挺有趣,是用sql统计字符串出现的次数. 这里提供一种思路,比如统计字符串A中子串B的出现次数: SELECT (LENGTH(A) - LENGTH(REPLACE(A, ...
- ORACLE 统计文件后缀SQL
ORACLE 统计文件后缀SQL查询语句 select lower(substr(file_name,instr(file_name, '.', -1),length(file_name))),cou ...
- SQL Server中的SQL语句优化与效率问题
很多人不知道SQL语句在SQL SERVER中是如何执行的,他们担心自己所写的SQL语句会被SQL SERVER误解.比如: select * from table1 where name='zhan ...
- SQL Server Profiler监控SQL Server性能
全面掌握SQL Server Profiler 1. 原理与相关概念介绍 SQL Server Profiler,大家已经非常熟悉.常常在性能优化中使用,本文档详细介绍SQL Server ...
- 整理:sql语句优化之SQL Server
. 增加服务器CPU个数;但是必须明白并行处理串行处理更需要资源例如内存.使用并行还是串行程是MsSQL自动评估选择的.单个任务分解成多个任务,就可 以在处理器上运行.例如耽搁查询的排序.连接.扫描和 ...
随机推荐
- EXTJS 4.2 资料 控件之combo 联动
写两个数据源: 1.IM_ST_Module.js { success:true, data:[ { ModuleId: '1', ModuleName: '资讯' } , { ModuleId: ' ...
- AirDrop显示名字的修改问题
AirDrop的名字来源是设备登陆的iCloud账户 打开iCloud设置 把个人信息的名字改成自己的即可 前提是你的账号没有借朋友用过,如果朋友用过恰好没注销,你的通讯录又有你的朋友的号码,很有可能 ...
- Windows下配置使用 MemCached
Windows下配置使用MemCached 工具: memcached-1.2.6-win32-bin.zip MemCached服务端程序(for win) Memcached Manage ...
- spring mvc中的@PathVariable(转)
鸣谢:http://jackyrong.iteye.com/blog/2059307 ------------------------------------------------ spring m ...
- hdu 4403
水水的dfs #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath& ...
- 注入攻击-SQL注入和代码注入
注入攻击 OWASP将注入攻击和跨站脚本攻击(XSS)列入网络应用程序十大常见安全风险.实际上,它们会一起出现,因为 XSS 攻击依赖于注入攻击的成功.虽然这是最明显的组合关系,但是注入攻击带来的不仅 ...
- 使用session技术来实现网上商城购物车的功能
首先.简单的了解session和cookie的区别: 一.session和cookie的区别: session是把用户的首写到用户独占的session中(服务器端) cookie是把用户的数据写给用户 ...
- ZOJ1204——Additive equations(DFS)
Additive equations Description We all understand that an integer set is a collection of distinct int ...
- Android EditText属性
1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: 把该EditText设为:android:password="true" // 以”.”形式显示文本 ( ...
- git clone 出错SSL certificate problem, verify that the CA cert is OK.
先调用这个 export GIT_SSL_NO_VERIFY=true 之后再执行git clone