日期:2014-05-16  浏览次数:20880 次

MySql 简单分组统计 如何实现?
数据结构

PCode PCount
5________1
6________0
7________0
5________1
6________2
7________0
5________0
6________2
7________0
6________2
8________1

结果

PCode PCount
5________2
6________6
7________0
8________1

------解决方案--------------------
SQL code
select PCode, PCount=sum(PCount) from tb group by PCount;

------解决方案--------------------
SQL code

--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
create table [test]([PCode] int,[PCount] int)
insert [test]
select 5,1 union all
select 6,0 union all
select 7,0 union all
select 5,1 union all
select 6,2 union all
select 7,0 union all
select 5,0 union all
select 6,2 union all
select 7,0 union all
select 6,2 union all
select 8,1

select [PCode],SUM([PCount]) as [PCount] 
from test
group by [PCode]
/*
PCode    PCount
5    2
6    6
7    0
8    1
*/