1

我在 Oracle SQL 中有两个表:

项目(PID,Pname,预算,DID

部门(DID,Dname)

粗体= 主键

斜体= 外键

我想列出项目比部门营销多的部门。

这是我的代码:

select dname as "Division"
from division d, project p
where d.did = p.did
group by dname
having count(pid) >= all
(select count(p.pid)
from project p, division d
where p.did = d.did and d.dname = 'marketing')  

我返回了正确的记录,但也返回了营销记录。如何从结果中排除营销记录?

4

2 回答 2

3

为什么不通过添加以下内容从初始 SQL 中排除营销记录:

and d.dname != 'marketing'

到第where一句。

于 2012-12-15T22:55:34.783 回答
0

您可能会使用公用表表达式(WITH 子句)来按部门聚合计数来提高效率,然后您可以查询它...

with cte as (
  select   dname,
           count(*) projects
  from     project p,
           division d
  where    p.did = d.did
  group by dname)
select dname,
       projects
from   cte
where  projects > (select projects
                  from   cte
                  where  dname = 'Marketing)
于 2012-12-15T21:23:27.897 回答