首先,我真的不知道如何提出我的问题。
数据库里有不同的公司。
我想知道哪些公司没有“分析师”档案。
以下是我的疑问:
select
t.name as "name"
t.pre as "first name"
t.id as "# account"
t.profile as "Profile"
b.cod_miss as "Mission"
b.df_missn as "End date"
from sr.v t
inner join od.e_lds on t.niu_ld = b.niu_ld
where b.cod_miss = 'APPROV'
and t.profile = 'Analyst'
这个查询为我的数据库中的每个公司提供了所有分析师。但我希望所有没有分析师的公司。我该怎么做?我试着用‘和t.profile <>’分析师“,”但很明显这不是很好.
编辑
我尝试了接受的答案,但我注意到它只是返回给我每个不是分析师的人。
假设有一家X公司有3名员工。其中一个是分析师。我不希望那家公司在我的结果上有所出格。但如果Y公司有2名员工,而且没有一名是“分析师”,那么我希望这家公司能在结果中得出结果。
发布于 2021-12-07 19:14:16
如果我没听错的话,那就是not exists
。就像这样:
select *
from sr.v
where not exists (select null
from od.e_lds b
where b.niu_ld = t.niu_ld
and t.profile = 'Analyst'
);
应用于查询:
select
t.name as "name"
t.pre as "first name"
t.id as "# account"
t.profile as "Profile"
b.cod_miss as "Mission"
b.df_missn as "End date"
from sr.v t inner join od.e_lds b on t.niu_ld = b.niu_ld
where b.cod_miss = 'APPROV'
--
and not exists (select null
from od.e_lds c
where c.niu_ld = t.niu_ld
and t.profile = 'Analyst'
);
编辑#2,带有一些示例数据
这是一个例子,展示了你试图用文字解释的东西(不过,如果你贴出样本数据会更好)。正如你所看到的,宝马的一名员工是分析师,而SAP中没有人是>>,因此,SAP被退回。
SQL> with test (company, ename, profile) as
2 (select 'BMW', 'Scott', 'Analyst' from dual union all
3 select 'BMW', 'King' , 'Manager' from dual union all
4 select 'BMW', 'Mike' , 'Clerk' from dual union all
5 --
6 select 'SAP', 'John' , 'Clerk' from dual union all
7 select 'SAP', 'Fred' , 'Manager' from dual
8 )
9 select a.company, a.ename, a.profile
10 from test a
11 where not exists (select null
12 from test b
13 where b.company = a.company
14 and b.profile = 'Analyst');
COMPANY ENAME PROFILE
---------- ----- -------
SAP Fred Manager
SAP John Clerk
SQL>
https://stackoverflow.com/questions/70265731
复制相似问题