--创建测试表
CREATE TABLE TEST(a VARCHAR2(8),b NUMBER);
INSERT INTO TEST
SELECT 'name1',1 FROM dual
UNION
SELECT 'name1',0 FROM dual
UNION
SELECT 'name2',1 FROM dual
UNION
SELECT 'name3',1 FROM dual
UNION
SELECT 'name4',0 FROM dual
UNION
SELECT 'name4',1 FROM dual;
--查询出表中字段A中为1的多少个,为0的多少个
SELECT a,count(*) as 总行数,
sum(case when b=1 then 1 else 0 end) as 类型为1的数量,
sum(case when b=0 then 1 else 0 end) as 类型为0的数量
from TEST
GROUP by a ORDER BY a;
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22918188/viewspace-1054000/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/22918188/viewspace-1054000/