简体   繁体   English

Not exists 子句未按预期工作

[英]Not exists clause isn't working as expected

I want to search every company ( niu_lds ) that does NOT have any Analyst ( cod_role_acces ) working for it.我想搜索没有任何分析师 ( cod_role_acces ) 为其工作的每一家公司 ( niu_lds )。

Example: Let's say there is a company X with 3 employees.示例:假设有一家公司 X,有 3 名员工。 One of them is an analyst.其中一位是分析师。 I would not want that company to come up in my results.我不希望那家公司出现在我的结果中。 but if there is company Y with 2 employees, and none of them is an "analyst", then I would like this company to come up in the result.但是,如果 Y 公司有 2 名员工,而且他们都不是“分析师”,那么我希望这家公司出现在结果中。

This is my query:这是我的查询:

SELECT DISTINCT
    a.name_pers as "First Name",
    a.last_pers as "Last Name",
    a.id as "# account",
    a.cod_role_acces as "Profile", 
    a.niu_lds as "Company", 
    b.cod_missn as "Mission", 
    
FROM sr.c_r_v_obt_cp_util a inner join ods.c_od_missn_ld b on a.niu_lds = b.niu_lds

WHERE a.cod_eta_util in ('VER', 'APPR')
and a.id_cod_sr_alim = '2'
and b.cod_missn = 'PHA'

 and not exists (select null
                 from sr.c_r_v_obt_cp_util c 
                 where c.niu_lds = a.niu_lds
                 and a.cod_role_acces = 'ANALYST'
                );

My problem is it's returning me "company" that HAVE "analyst(s)".我的问题是它让我拥有“分析师”的“公司”。 I want to see all the company (niu_lds) that has no 'ANALYST'(cod_role_access) attributed to said company.我想查看所有没有归属于该公司的“分析员”(cod_role_access) 的公司 (niu_lds)。

I already asked the question, and someone helped me a lot but I'm still not getting the expected output.我已经问过这个问题,有人帮了我很多,但我仍然没有得到预期的 output。

my first question: SQL query to check if a value isn't present我的第一个问题: SQL 查询以检查值是否不存在

Thank you谢谢

I believe the answer from Romeo with changing the null to 1 might get you there.我相信罗密欧的答案是将 null 更改为 1 可能会让你到达那里。

If it doesn't and you want to see visually what's happening to compare, you could turn the not exists function into a left join so you can see where values are getting placed.如果没有,并且您想直观地查看比较发生了什么,您可以将不存在的 function 转换为左连接,以便您可以看到放置值的位置。

Using the test example from the previous post -使用上一篇文章中的测试示例 -

with test (company, ename, profile) as
      (select 'BMW', 'Scott', 'Analyst' union all
       select 'BMW', 'King' , 'Manager' union all
      select 'BMW', 'Mike' , 'Clerk'  union all
      select 'SAP', 'John' , 'Clerk' union all
      select 'SAP', 'Fred' , 'Manager' 
    )
  select a.company, a.ename, a.profile, b.analyst
  from test a
  left join (select 1 as analyst, company
        from test
        where profile = 'Analyst') b
        using(company)

The following is the return -以下是回报——

COMPANY公司 ENAME姓名 PROFILE轮廓 ANALYST分析师
BMW宝马 Scott斯科特 Analyst分析师 1 1
BMW宝马 King国王 Manager经理 1 1
BMW宝马 Mike麦克风 Clerk文员 1 1
SAP树液 John约翰 Clerk文员 null null
SAP树液 Fred弗雷德 Manager经理 null null

You can see that all of BMW is getting marked for Analyst, from here, if you wanted to build off the left join rather than not exists but 'hide' the column marking Analyst, add 'where b.analyst is null' and remove b.analyst from the select statement -您可以看到所有 BMW 都被标记为 Analyst,从这里开始,如果您想构建左连接而不是不存在但“隐藏”标记为 Analyst 的列,添加“where b.analyst is null”并删除 b .来自 select 声明的分析师 -

with test (company, ename, profile) as
      (select 'BMW', 'Scott', 'Analyst' union all
       select 'BMW', 'King' , 'Manager' union all
      select 'BMW', 'Mike' , 'Clerk'  union all
      select 'SAP', 'John' , 'Clerk' union all
      select 'SAP', 'Fred' , 'Manager' 
    )
  select a.company, a.ename, a.profile
  from test a
  left join (select 1 as analyst, company
        from test
        where profile = 'Analyst') b
        using(company)
where b.analyst is null

This returns the expected table -这将返回预期的表 -

COMPANY公司 ENAME姓名 PROFILE轮廓
SAP树液 John约翰 Clerk文员
SAP树液 Fred弗雷德 Manager经理

You can try the code below;你可以试试下面的代码;

WITH all_roles AS (
SELECT *,
      SUM(CASE WHEN cod_role_acces = 'ANALYST' THEN 1 ELSE 0 END) OVER (PARTITION BY niu_lds ORDER BY niu_lds ) AS rolecount
FROM sr.c_r_v_obt_cp_util
)
SELECT DISTINCT
    a.name_pers as "First Name",
    a.last_pers as "Last Name",
    a.id as "# account",
    a.cod_role_acces as "Profile", 
    a.niu_lds as "Company", 
    b.cod_missn as "Mission"
FROM all_roles a inner join ods.c_od_missn_ld b on a.niu_lds = b.niu_lds
WHERE a.cod_eta_util in ('VER', 'APPR')
AND a.id_cod_sr_alim = '2' AND b.cod_missn = 'PHA'
AND a.rolecount < 1;

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

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