我现在对表的一个字段求和得到各班的人数和在这基础上在对年级人数求和。       班级   人数 年级人数     
       1000    20     50
       1001    30     50
       2000    10     30
       2001    20     30
这要怎么用sql语句实现啊,谢谢!最好给出代码!

解决方案 »

  1.   

    select sum(人数) as cnt
    from your_table
    group by subStr(班级,1,1)
      

  2.   

    比如你现在的表是:
     班级   人数 年级人数     
    1000    20     50
    1001    30     50
    2000    10     30
    2001    20     30select sum(人数) from table group by substr(班级,1,2)
    取班级的前2位
      

  3.   

    那么已经给出SQL了阿,具体怎样要自己调了
      

  4.   

    select t.*,v.c_nj
    from
    (
    select 班級,count(1) from tb
    group by 班級) t,
    (select substr(班級,1,1) nj,count(1) c_nj from tb
    group by substr(班級,1,1)
    )v
    where substr(t.班級,1,1)=v.nj