ORACLE 如何对COUNT()列的值求平均数

2025-01-24 09:38:58
推荐回答(2个)
回答1:

你这思路有点乱,应该是两个查询:

 

  1. SELECT G."ID",G."NAME",COUNT(*)
    FROM "GROUPS" G,USERS U
    WHERE G."ID"=U."GROUPID"
    GROUP BY G."NAME",G."ID"

  2. select avg(cnt) from

    (SELECT G."ID",G."NAME",COUNT(*) cnt

    FROM "GROUPS" G,USERS U
    WHERE G."ID"=U."GROUPID"
    GROUP BY G."NAME",G."ID"

    ) tmp

回答2:

1、count(*)==count(1)再avg肯定是1,没有意义
2、select g.id,g.name,count(*),sum(col1),avg(col2) from groups g,users u where g.id=u.groupid group by g.name,g.id;