数据结构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

解决方案 »

  1.   

    select PCode, PCount=sum(PCount) from tb group by PCount;
      

  2.   


    --> 测试数据:[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,1select [PCode],SUM([PCount]) as [PCount] 
    from test
    group by [PCode]
    /*
    PCode PCount
    5 2
    6 6
    7 0
    8 1
    */