Oracle SQL - Not Exists - string does not exist in a list of values

◇◆丶佛笑我妖孽 提交于 2019-12-13 04:42:01

问题


Background: For a SPRIDEN_id, sprhold_hldd_code may have one or more of several values or NO values.

I need to select SP.SPRIDEN_ID, SP.SPRIDEN_LAST_NAME, SP.SPRIDEN_FIRST_NAME, SR.SHRDGMR_SEQ_NO, SR.SHRDGMR_PROGRAM

Where (sh.sprhold_hldd_code = 'RH') does not exist.

So far, no records are returned.

I have found that if I put a code that is not in the list of possible values (such as Z) in sh.sprhold_hldd_code = 'Z', then it will return results.

DATA: (column names abbreviated)
SPRIDEN_ID   SPRIDEN_LAST   SPRIDEN_FIRST  SHRDGMR_SEQ_NO   SHRDGMR_PROGRAM sh.sprhold_hldd_code
100001       Smith          Sue            1                ALHE            RH
100001       Smith          Sue            1                ALHE            AA
100001       Smith          Sue            1                ALHE            BB
100005       Conners        Tim            1                BUSN            RH
100008       Occent         Mary           1                MATH            CC
100008       Occent         Mary           1                MATH            AA

Expected Results: Mary is the only record that does NOT have the RH code.

SPRIDEN_ID   SPRIDEN_LAST_NAME   SPRIDEN_FIRST_NAME  SHRDGMR_SEQ_NO  SHRDGMR_PROGRAM
100008       Occent              Mary                1                MATH              

I receive no results though.

CODE:
Select SP.SPRIDEN_ID, SP.SPRIDEN_LAST_NAME, SP.SPRIDEN_FIRST_NAME, SR.SHRDGMR_SEQ_NO,          SR.SHRDGMR_PROGRAM
from spriden SP
join SHRDGMR SR on SP.SPRIDEN_PIDM = SR.SHRDGMR_PIDM
join SPRHOLD SH on sp.spriden_pidm = sh.sprhold_pidm
where SR.SHRDGMR_DEGS_CODE = 'PN'
  and SR.SHRDGMR_TERM_CODE_GRAD >= '201489'
  and sp.spriden_change_ind is NULL
and not exists
(select sh.sprhold_pidm
from sprhold sh
where sh.sprhold_hldd_code = 'RH')

回答1:


To get the data you want, I would recommend using aggregation with a having clause:

Select SP.SPRIDEN_ID, SP.SPRIDEN_LAST_NAME, SP.SPRIDEN_FIRST_NAME, SR.SHRDGMR_SEQ_NO, 
       SR.SHRDGMR_PROGRAM
from spriden SP join
     SHRDGMR SR
     on SP.SPRIDEN_PIDM = SR.SHRDGMR_PIDM join
     SPRHOLD SH
     on sp.spriden_pidm = sh.sprhold_pidm
where SR.SHRDGMR_DEGS_CODE = 'PN' and
      SR.SHRDGMR_TERM_CODE_GRAD >= '201489' and
     sp.spriden_change_ind is NULL
group by SP.SPRIDEN_ID, SP.SPRIDEN_LAST_NAME, SP.SPRIDEN_FIRST_NAME, SR.SHRDGMR_SEQ_NO, 
         SR.SHRDGMR_PROGRAM
having sum(case when sh.sprhold_hldd_code = 'RH' then 1 else 0 end) = 0;

You have two problems with your approach. The first is that the subquery either returns true or false and affects all rows in the original query. You really want a correlated subquery. But, even if you got that right, you would be returning duplicate rows for Mary. This solves both those problems.




回答2:


you can do small change and try

and sh.sprhold_pidm not in
(select sh.sprhold_pidm
from sprhold sh
where sh.sprhold_hldd_code = 'RH')


来源:https://stackoverflow.com/questions/26003192/oracle-sql-not-exists-string-does-not-exist-in-a-list-of-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!