MySql 简单分组统计 如何实现?数据结构PCodePCount5________16________07________05________16________27_
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 allselect 6,0 union allselect 7,0 union allselect 5,1 union allselect 6,2 union allselect 7,0 union allselect 5,0 union allselect 6,2 union allselect 7,0 union allselect 6,2 union allselect 8,1select [PCode],SUM([PCount]) as [PCount] from testgroup by [PCode]/*PCode PCount5 26 67 08 1*/ 