我有一个从多个表中提取数据的查询,其中一个名为 empl_access
.我只需要只有 s_system_cd
的在职员工WEC
的类型.
此字段有两个值:WEC
和 WTC
.员工可以同时拥有两者,但我希望员工只有一个,WEC
.
到目前为止,这是我的脚本。这拉动了所有 WEC
行,但其中一些也有 WTC
我希望它们被过滤掉。
select DISTINCT a.empl_id, a.last_name, a.first_name, a.active_fl,
b.UDT09_ABBRV_ID, c.s_system_cd, count(a.empl_id) over ()
from empl a, V_TBE_MAX_EMPL_HISTORY b, empl_access c
where a.empl_id=b.empl_id
and a.empl_id=c.empl_id
and a.active_fl='Y'
and c.s_system_cd like 'WEC%'
order by a.empl_id;
这是表中内容的示例:
empl_id |姓氏 |名字| udt09_abbr_id | s_system_cd
-----------|----------------|----------------|---- -----------------|-----------------
000000 |鼠标 |米奇 |第1111章世界经济委员会
-----------|----------------|----------------|---- -----------------|-----------------
000000 |鼠标 |米奇 |第1111章世贸中心
-----------|----------------|----------------|---- -----------------|-----------------
000010 |鸭 |唐纳德 |第1111章世界经济委员会
-----------|----------------|----------------|---- -----------------|-----------------
000010 |鸭 |唐纳德 |第1111章世贸中心
-----------|----------------|----------------|---- -----------------|-----------------
000020 |派克 |约翰 | 1150 |世界经济委员会
-----------|----------------|----------------|---- -----------------|-----------------
000030 |史密斯 |安妮 |第1152章世界经济委员会
这是我想拉的
-----------|----------------|----------------|---- -----------------|-----------------
empl_id |姓氏 |名字| udt09_abbr_id | s_system_cd
-----------|----------------|----------------|---- -----------------|-----------------
000020 |派克 |约翰 | 1150 |世界经济委员会
-----------|----------------|----------------|---- -----------------|-----------------
000030 |史密斯 |安妮 |第1152章世界经济委员会
-----------|----------------|----------------|---- -----------------|-----------------
最佳答案
这样的事情会有帮助吗?
SQL> with test (empl_id, s_system_cd) as
2 (select 1, 'WEC' from dual union all -- EMPL_ID = 1 has both - we don't want it
3 select 1, 'WTC' from dual union all
4 select 2, 'WTC' from dual union all -- EMPL_ID = 2 has only WTC - we don't want it
5 select 3, 'WEC' from dual union all -- EMPL_ID = 3 has only WEC - we do want it
6 select 4, 'WEC' from dual union all -- EMPL_ID = 4 has WEC, twice - we do want it
7 select 4, 'WEC' from dual
8 )
9 select empl_id
10 from test
11 group by empl_id
12 having min(s_system_cd) = max(s_system_cd)
13 and min(s_system_cd) = 'WEC'
14 order by empl_id;
EMPL_ID
----------
3
4
SQL>
关于Sql 查询以查找只有一行的行(对于 exp 许可证),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52654604/