码迷,mamicode.com
首页 > 数据库 > 详细

sql type subtype 统计

时间:2017-06-13 17:14:11      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:type   when   end   style   ase   from   bsp   test   table   

select * from testtable;

 

type subtype value

a sa 1
b sb 1
a sb error
b sa error
a sb 1
b sb 1
c sa error
c sb 1
c sc 1
c sc 1
c sa error


select sum(`value`) from testtable where value != ‘error‘ AND type =‘b‘;


sum(`value`) 

2


SELECT type,subtype,SUM(value) AS TOTNUM FROM testtable WHERE value !=‘error‘ GROUP BY type,subtype;

type subtype TOTNUM

a sa 1
a sb 1
b sb 2
c sb 1
c sc 2
c sc 2


SELECT type,subtype,COUNT(value) AS TOTCOUNT FROM testtable WHERE value =‘error‘ GROUP BY type,subtype;

type subtype TOTCOUNT

a sb 1
b sa 1
c sa 2
c sa 2


select * from (select type,subtype,sum(case when value <> ‘error‘ then value else ‘0‘ end) as 总数 ,sum(case when value = ‘error‘ then 1 else ‘0‘ end) as error行数 from testtable group by type,subtype) t ORDER BY 总数 desc

 

type subtype 总数 error行数

 

c sc 2 0
b sb 2 0
a sa 1 0
c sb 1 0
a sb 1 1
c sa 0 2
b sa 0 1

sql type subtype 统计

标签:type   when   end   style   ase   from   bsp   test   table   

原文地址:http://www.cnblogs.com/dyfisgod/p/7001012.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!