table:  Testid   score
1     10
1     20
2     30
2     40
3     50
希望查询结果为:
id  score  _sum  
1     30     5
2     70     5
3     50     5    其中_sum的数值是记录的条数  请教各位高手下这个SQL该怎么写啊,由于数据比较多,希望能够高效。

解决方案 »

  1.   

    你用count和sum这两个函数来解决
      

  2.   


    select id,sum(score),count(id) from 表 group by id
      

  3.   

    看样子楼主要的是总纪录数,不是分组的每条纪录数
    用这样的
    select id,sum(score),count(id) over() from 表 group by id
      

  4.   

    select t1.id,sum(t1.score),count(*) from table t1 group by t1.id;
    试试看,
      

  5.   

    要用OVER统计才对吧,单用count是不行的
    select id,sum(score),count(*) over()sum  from tablename group by id
      

  6.   

    select id,sum(score),count(1) from tablename group by id order by id
      

  7.   


    select id,sum(score),count(id) over() from 表 group by id
      

  8.   

    select id,sum(score),count(id) over() from 表 group by id 
      

  9.   

    select id,sum(score),count(id) over() from 表 group by id
    在test这个表中 (假设NO为PK)
    id  score   no
    1    10      a
    1    20      b
    2    30      c
    2    40      d
    3    50      e
    over()里面该怎么写才能得到它的总条数的呀?
      

  10.   

    over里什么都不写,就是总条数