Sql 计算在校总人数

Sql 计算在校总人数,sql,oracle,Sql,Oracle,学校的数据库主要有两个表:学生表和教师表 表的创建几乎是这样的 create table students ( students_id int, year int ) create table Teacher ( Teacher_id int, year int ) 创建表格学生( 学生(国际), 整年 ) 创建表格教师( 教师id int, 整年 ) 但是我知道每年的总人数(学生+老师)是多少 范例 students students_id

学校的数据库主要有两个表:学生表和教师表 表的创建几乎是这样的

create table students ( students_id int, year int ) create table Teacher ( Teacher_id int, year int ) 创建表格学生( 学生(国际), 整年 ) 创建表格教师( 教师id int, 整年 ) 但是我知道每年的总人数(学生+老师)是多少

范例

students students_id year 01 2020 02 2019 03 2020 04 2019 Teacher Teacher_id year 01 2020 02 2018 03 2020 04 2019 answer year total 2020 4 (2 student + 2 Teacher) 2019 3 (2 student + 1 Teacher) 2018 1 (0 student + 1 Teacher) 学生 学生身份证年度 01 2020 02 2019 03 2020 04 2019 老师 教师身份证年度 01 2020 02 2018 03 2020 04 2019 回答 全年总数 2020 4(2名学生+2名教师) 2019 3(2名学生+1名教师) 2018年1(0名学生+1名教师)
使用union all和聚合:

select year, count(*)
from ((select year from students) union all
      (select year from teachers)
     ) st
group by year;