首先,我真的不知道如何表述我的問題。
我在一個資料庫中有不同的公司。
我想知道哪些公司沒有“分析師”簡介。
這是我的查詢:
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'
此查詢為我提供了資料庫中每家公司的所有分析師。但我想要所有沒有任何分析師的公司。我該怎么做?我嘗試使用 'and t.profile <> 'analyst' " 但顯然這不能很好地作業......
編輯:
我嘗試了接受的答案,但我注意到它只會讓我不是分析師的每個人都回傳給我。
假設有一家公司 X,有 3 名員工。其中之一是分析師。我不希望那家公司出現在我的結果中。但是如果Y公司有2名員工,而他們都不是“分析師”,那么我希望這家公司出現在結果中。
uj5u.com熱心網友回復:
如果我理解正確的話,那就是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,帶有一些示例資料]
這是一個示例,顯示了您試圖用文字解釋的內容(不過,如果您發布示例資料會更好)。如您所見,BMW 的一名員工是分析師,而 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://www.uj5u.com/shujuku/376659.html
上一篇:選擇從當前日期起14天的所有記錄
