我有个借书的表 有读者编号 书编号 我想求所有读者的平均读几本书, 但是如果写成 avg(count(*))这样是错误的 
请问应该怎么查询阿 谢谢回答~~~~~~~~~

解决方案 »

  1.   

    declare  @t table(书编号 int ,读者编号 int)
    insert into @t 
    select 1,1 union all 
    select 1,2 union all 
    select 2,2 union all 
    select 3,3 union all 
    select 2,3  select count(*)*1.0/count(distinct 读者编号)  from @t
    --1.666666666666
      

  2.   


    select avg(QTY) from 
    (select count(1) as QTY,Reader from TB group by Reader) a
      

  3.   

    请问count(*)一定要乘以1.0吗 
      

  4.   


    不一定,也可以使用cast转化为小数。