平均分组

时间:2015-10-03 01:00:51

标签: sql sql-server sql-server-2008

尝试计算每位会员借阅的平均书籍数量。
会员( memb no ,姓名,年龄)
书( isbn ,标题,作者,出版商)
借( memb no,isbn ,date)
大胆 - 主键
斜体 - 外键
这里有人可以帮帮我吗?在此先感谢

4 个答案:

答案 0 :(得分:1)

要获得每个成员借来的不同书籍的数量,你可以做到

select membno, count(*) as totalborrowed
from borrowed
group by membno

要获得会员总数,您可以

select count(distinct membno) as totalmembers
from borrowed

要获得平均借书,您应该将结果合并。与1.0相乘得到小数结果。

select 1.0 * sum(totalborrowed)/count (distinct b.membno) as avg_per_member
from
(select membno, count(*) as totalborrowed
from borrowed
group by membno) t join borrowed b on t.membno = b.membno

答案 1 :(得分:0)

select AVG(MemberBookCount)
from (
    select count(*) * 1.0 as MemberBookCount
    from MemberBook
    group by Member
) a

答案 2 :(得分:0)

SELECT COUNT (*)  / CAST((SELECT COUNT (*) FROM member) AS FLOAT)
FROM borrowed

答案 3 :(得分:0)

我认为最简单的方法是两个值的比率:

select count(*) * 1.0 / count(distinct membo)
from borrowed;