简体   繁体   中英

SQL - return rows that do not have a certain value

looking for a bit of help here if possible?

I have the following query:-

On or database we have a table called Linkfile, in this table are "Types" all beginning with "YG". I need to return those rows that do not have the type of "YG8" but just cannot seem to do it. I know ill need to use a sub query but am stuck!

This is my code and the fields I need to return. I just need to only show those that do not have the lk.type of "YG8"

select distinct l.description, p.displayname AS Temp,      p.compliance_status      As 'Compliant', lk.displayname, lk.type
from event e 
inner join organisation o on e.organisation_ref = o.organisation_ref
inner join opportunity opp  on e.opportunity_ref = opp.opportunity_ref
inner join event_role ev on ev.event_ref = e.event_ref
inner join address a on a.address_ref = opp.address_ref
inner join person p on ev.person_ref = p.person_ref
inner join lookup l on p.responsible_team = l.code
inner join person_type pt on  p.person_ref = pt.person_ref
inner join linkfile lk on lk.parent_object_ref = pt.person_ref
where  o.displayname LIKE '%G4S%'   and p.compliance_category = '$016' 
and lk.type like 'YG%'  and l.code_type = '2'
and a.displayname LIKE '%MOJ%' 
and pt.status = 'A'
order by l.description,  p.displayname, lk.type

Use below query :

select distinct l.description, p.displayname AS Temp,      p.compliance_status      As 'Compliant', lk.displayname, lk.type,lk.parent_object_ref
from event e 
inner join organisation o on e.organisation_ref = o.organisation_ref
inner join opportunity opp  on e.opportunity_ref = opp.opportunity_ref
inner join event_role ev on ev.event_ref = e.event_ref
inner join address a on a.address_ref = opp.address_ref
inner join person p on ev.person_ref = p.person_ref
inner join lookup l on p.responsible_team = l.code
inner join person_type pt on  p.person_ref = pt.person_ref
left join (select displayname, type,parent_object_ref from linkfile where lk.type like 'YG8%'  )lk on lk.parent_object_ref = pt.person_ref
where  o.displayname LIKE '%G4S%'   and p.compliance_category = '$016' and lk.parent_object_ref is null
 and l.code_type = '2'
and a.displayname LIKE '%MOJ%' 
and pt.status = 'A'
order by l.description,  p.displayname, lk.type;

I've used left join on linkfile with type like 'YG8%' and fetching the only records which are not matched

I think you can just replace the

lk.type like 'YG%' 

with the following:

(lk.type >= 'YG' and lk.type <'YG8') or (lk.type > 'YG8' and lk.type <='YGZ') 

this should accomplish what you are trying to do and also avoid using "like" which is less efficient (assuming you have an index on lk.type, at least).

You may refine this a bit by knowing which are the possible values of lk.type of course. Ie what are the extremes for the YG "subtype"? YG00-YG99? YG-YGZ?

(Be especially careful if you may have YG81 or YG87 for example, because then my clause will not work properly... on the other hand if your YG subtype can have values like YG34 it would have been better to use YG08 instead of YG8)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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