简体   繁体   English

Oracle SQL从查询结果中排除行

[英]Oracle SQL Exclude row from query results

I have two tables in Oracle SQL: 我在Oracle SQL中有两个表:

PROJECT ( PID , Pname, Budget, DID ) 项目( PID ,Pname,预算, DID

DIVISION ( DID , Dname) DIVISION( DID ,Dname)

Bold = Primary key 粗体 =主键

Italic = Foreign key 斜体 =外键

I want to list the division that has more projects than the division marketing. 我想列出比分部营销有更多项目的部门。

Here is my code: 这是我的代码:

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')  

I return the correct record but also the marketing record. 我返回正确的记录,但也返回营销记录。 How can I exclude the marketing record from the results? 如何从结果中排除营销记录?

Why don't you exclude the marketing record from your initial SQL by adding: 为什么不通过添加以下内容从初始SQL中排除营销记录:

and d.dname != 'marketing'

To the first where clause. 到第一个where子句。

You might gain efficiency with a common table expression (WITH clause) to aggregate the count by department, then you can query it ... 您可以使用公共表表达式(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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM