How to seek a NOT IN RANGE condition

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • roslyn
    New Member
    • May 2010
    • 1

    How to seek a NOT IN RANGE condition

    I'm trying to build a report which highlights the Inactive Suppliers within the ERP database and have the following query which I've built but am struggling to find/apply a "not in range" condition.

    SELECT DISTINCT oas_docline.el2 AS [Supplier Code], oas_element.nam e AS [Supplier Name], oas_element.add date AS [Date Created], oas_element.mod date AS [Last Modified Date], oas_element.usr name AS [Modified By], oas_element.cur , oas_element.ter ms AS Terms, IIf([elec]=76,"Yes","") AS [Electronic Payments], IIf([paper]=75,"Yes","") AS [Paper Payments], oas_element.med code AS [Payment Method], IIf([defbank]=1,"Yes","No") AS [Default Bank], IIf([elec]=76,[bankname],"") AS Bank, IIf([elec]=76,[acname],"") AS [Account Name], IIf([elec]=76,[acref],"") AS [Account Ref], IIf([elec]=76,oas_elmbank list.sort,"") AS SortCode, IIf([elec]=76,[acnum],"") AS [Account Number]
    FROM (((oas_docline INNER JOIN oas_dochead ON (oas_docline.do cnum = oas_dochead.doc num) AND (oas_docline.do ccode = oas_dochead.doc code) AND (oas_docline.cm pcode = oas_dochead.cmp code))
    INNER JOIN oas_element ON oas_docline.el2 = oas_element.cod e)
    INNER JOIN oas_elmaddrlist ON (oas_element.el mlevel = oas_elmaddrlist .elmlevel) AND (oas_element.co de = oas_elmaddrlist .elmcode) AND (oas_element.cm pcode = oas_elmaddrlist .cmpcode))
    INNER JOIN oas_elmbanklist ON (oas_element.el mlevel = oas_elmbanklist .elmlevel) AND (oas_element.co de = oas_elmbanklist .elmcode) AND (oas_element.cm pcode = oas_elmbanklist .cmpcode)
    WHERE (((oas_docline. el2) Like [P%,E%,or %]) AND ((oas_element.a dddate) Is Not Null) AND ((oas_element.c mpcode)="MASTER 11")
    AND ((oas_dochead.y r)>=2008) AND ((oas_dochead.s tatus)<>79)
    AND ((oas_element.e lmlevel)=2) AND ((oas_element.d eldate) Is Null)
    AND ((oas_element.m atchable)=76)
    AND ((oas_element.c ustsuppacc)=76)
    AND ((oas_element.e ndyear)<=0))
    OR (((oas_docline. el2) Like [P%,E%,or %])
    AND ((oas_element.a dddate) Is Not Null)
    AND ((oas_element.c mpcode)="MASTER 11")
    AND ((oas_dochead.y r)<=2010)
    AND ((oas_dochead.s tatus)<>79)
    AND ((oas_element.e lmlevel)=2)
    AND ((oas_element.d eldate) Is Null)
    AND ((oas_element.m atchable)=76)
    AND ((oas_element.c ustsuppacc)=76)
    AND ((oas_element.e ndyear)<=0))
    ORDER BY oas_docline.el2 , oas_element.med code DESC;


    The above code works for where BETWEEN YYYY and YYYY is specified but I'd like to flip it on it's head and do a NOT IN RANGE query. My concern being that if I simply do a NOT BETWEEN it might bring back data outside that range but some of which may actually exist within the range specified, and I want the results to only bring back el2 codes which exist in oas_element but that aren't used/found in the date range specified on the dochead/docline tables.

    Each time I try and specify OUTER JOIN it comes back with either an error indicating "circular reference" or something similar.


    Any suggestions would be warmly welcomed!

    Many thanks in advance
  • Delerna
    Recognized Expert Top Contributor
    • Jan 2008
    • 1134

    #2
    Thats one huge query and to try and unravel that and write it to do what you want, without any data, would be extremely difficult ...... for me at least.

    I can give a suggestion however. I hope you can make sense of it.

    You need the query to still select the data between YYYY and YYYY as you have stated.

    What you need to do is left join the "dochead/docline" combination to oas_element and then select the records where a field from "dochead/docline" is equal to null.

    If that field is null then it means there was no matching record in "dochead/docline" between your dates for the record in oas_element , and these are the records you want
    The field you choose to test for null does, of course, need to have the proviso that if there was a matching record, then it couldn't be null.
    In other words, you can't choose a field that might contain a null value in the table itself.

    Does that make sense?


    I would suggest that you pull the "dochead/docline" part of your query, along with its date range test, out into it's own query and then reference that query in this one. That should make it a bit easier to get your head around the main query

    You can always combine them into one query once you get it working properly.

    good luck

    Comment

    Working...