我想在示例中展示我的问题。 所以,我有学生,他们在不同的学校学习,我想按学校计算平均数,然后计算总平均数。
示例:
那么,我怎么能得到像这样的结果?
提前致谢!
还有一个选择 - 在一个查询中执行此操作会非常舒服。
答案 0 :(得分:1)
with
marks ( school, student, mark ) as (
select 'School 1', 'James' , 5 from dual union all
select 'School 1', 'Mark' , 4 from dual union all
select 'School 1', 'Howard', 5 from dual union all
select 'School 4', 'Jho' , 4 from dual union all
select 'School 5', 'Kate' , 5 from dual
),
a ( school, s_avg ) as (
select school, avg(mark)
from marks
group by school
)
select school, student, mark,
avg(mark) over (partition by school) as avg_by_school,
(select avg(s_avg) from a) as global_avg
from marks;
SCHOOL STUDENT MARK AVG_BY_SCHOOL GLOBAL_AVG
-------- ------- ---------- ------------- ----------
School 1 James 5 4.667 4.556
School 1 Mark 4 4.667 4.556
School 1 Howard 5 4.667 4.556
School 4 Jho 4 4.000 4.556
School 5 Kate 5 5.000 4.556
5 rows selected.