我有以下两种模式:
advisor(s_ID, i_ID)
instructor(ID, name, dept_name, credits)
我已按以下方式计算了每个不同i_ID的i_ID出现次数:
SELECT i_ID, count(*)
FROM advisor
GROUP BY i_ID;
每个i_ID都属于dept_name。如何获得每个部门名称的总计数?
示例:
i_ID count
21 3
23 4
27 1
29 5
假设i_ID 21,23属于讲师(ID,物理),i_ID 27,29属于讲师(ID,化学),那么输出将是:
dept_name count
physics 7(3+4)
chemistry 6(1+5)
答案 0 :(得分:2)
我希望这会有所帮助。
select dept name, 0 as total advised
from (select dept name from department
minus
select dept name
from instructor)
union
select dept name, count(*) as total advised
from instructor, advisor
where instructor.id = advisor.I ID
group by dept name
order by dept name;
答案 1 :(得分:0)
试试这个
select a.dept_name,count(*) as count from instructor a join advisor b
on a.ID=b.i_ID group by a.ID