Results 1 to 14 of 14
  1. #1
    Stan Denman is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    55

    pairing query

    In my DB I have a document type "CPYEVREQ" for a given source that needs to have a companion "MER". The former is a request for medical records, and the latter document type the fulfillment of that request. So "source" is going to be the doctor or clinic for which we want medical records. This code will pair these request and responses up nicely: "SELECT PDFParentBookmark.DocumentType, PDFParentBookmark.DocumentSource, PDFParentBookmark_1.DocumentSource, PDFParentBookmark_1.DocumentTypeFROM PDFParentBookmark AS PDFParentBookmark_1 INNER JOIN PDFParentBookmark ON PDFParentBookmark_1.DocumentSource = PDFParentBookmark.DocumentSource


    WHERE (((PDFParentBookmark.DocumentType)="CPYEVREQ") AND ((PDFParentBookmark_1.DocumentType)="MER"));
    "

    BUT I want to identify the document types "CPYEVREQ" for which we do NOT have a matching MER.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    SELECT * FROM PDFParentBookmark WHERE DocumentSource NOT IN (SELECT DocumentSource FROM PDFParentBookmark WHERE DocumentType = "MER") AND DocumentType = "CPYEVREQ"

    Is DocumentSource enough to associate records? Can't doctor or clinic have multiple requests? Don't you also need patient ID? Even then, can there be multiple requests for same patient?

    Last edited by June7; 02-07-2025 at 11:48 AM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    EDITED:
    Whilst using NOT IN should give you the correct output, it will be very slow if you have a large dataset as that approach cannot make use of indexing.
    There are are two alternatives: NOT EXISTS and a simple unmatched query (using the wizard) both of which should be far faster as they do make use of any indexes in the join / filter fields

    For more information, see my article Finding Unmatched Records
    Last edited by isladogs; 02-07-2025 at 02:48 PM.
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  4. #4
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    433
    Code:
    SELECT * FROM 
    (SELECT 
            PDFParentBookmark.DocumentType, 
            PDFParentBookmark.DocumentSource, 
            PDFParentBookmark_1.DocumentSource, 
            PDFParentBookmark_1.DocumentType, 
    FROM PDFParentBookmark AS PDFParentBookmark_1  
    LEFT JOIN PDFParentBookmark ON PDFParentBookmark_1.DocumentSource = PDFParentBookmark.DocumentSource 
    WHERE (((PDFParentBookmark.DocumentType)="CPYEVREQ") AND ((PDFParentBookmark_1.DocumentType)="MER"))) 
    WHERE ((DocumentType) Is Null);

  5. #5
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Just for info, the suggested answers in posts #2 and #4 are both invalid SQL statements
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  6. #6
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    433
    oooh, that sql is not tested.

    this one is tested:

    Code:
    SELECT *
    FROM PDFParentBookmark
    WHERE (((PDFParentBookmark.DocumentSource) Not In  
    (SELECT 
            PDFParentBookmark.DocumentSource AS Source  
    FROM PDFParentBookmark AS PDFParentBookmark_1  
    INNER JOIN PDFParentBookmark ON PDFParentBookmark_1.DocumentSource = PDFParentBookmark.DocumentSource 
    WHERE PDFParentBookmark.DocumentType="CPYEVREQ" AND PDFParentBookmark_1.DocumentType="MER")));

  7. #7
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    433
    post #3 sql is valid but does not produce the desired result.

  8. #8
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Touché
    I had tested mine and thought it gave the required output.
    As mentioned I see no reason for a self join in this case but perhaps I’m misunderstanding the purpose.

    EDIT: Ignore that. Just read post #1 properly. Out at the moment. Will look again later when I’m at my computer
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  9. #9
    Stan Denman is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    55
    Thanks for your response. That works!

  10. #10
    Stan Denman is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    55
    Good points all. I am trying to locate which requests for medical records have not been responded to. You are correct that my logic is based upon an assumption that there will be only one request for medical records for a given source. For example, lets say in fact records were request from Dr. Death multiple times, and he finally honors the request. Since we would have one MER and two CPYEVREQ this code would still "flag" the request as unfulfilled when in fact it was fulfilled. Would have been nice if these requests were dated but they are not. However, it is true we could have if there is an MER for a given source with a page number that is higher than a CPYEVREQ, then that CPYEVREQ cannot be consider as "unfulfilled".

  11. #11
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    OK - sorry about my earlier incorrect comments about self joins. I've now removed those

    I can confirm that the subquery in post #6 works but I would still avoid using NOT IN for the reasons I explained in post #3
    If you want to use a subquery, NOT EXISTS is more efficient (but I find that harder to write correctly)

    An alternative approach that is usually the most efficient of all is an unmatched query

    For example, start with the query from post #1 using aliases to shorten the SQL and save as qryMatchedPairs.

    Code:
    SELECT A.*, B.DocumentTypeFROM PDFParentBookmark AS A INNER JOIN PDFParentBookmark AS B ON A.DocumentSource = B.DocumentSource
    WHERE (((B.DocumentType)="MER") AND ((A.DocumentType)="CPYEVREQ"));
    Now do an unmatched query based on the table and qryMatchedPairs:

    Code:
    SELECT T.*FROM PDFParentBookmark AS T LEFT JOIN qryMatchedPairs ON T.DocumentSource = qryMatchedPairs.DocumentSource
    WHERE (((qryMatchedPairs.B.DocumentType) IsNull));

    That gives the same output as the subquery in post #6 (Checked!)

    It could be written as a single query but it would be harder to read (and the OP already has the matched pairs query anyway!)
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Which works?

    I fixed the SQL in post 2.

    A "FIND UNMATCHED" is not so simple in this case because only 1 table is involved - this means a self-join and in this case I think a nested query is still needed.

    Colin, your link in post 3 isn't working.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  13. #13
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    @June7
    Thanks for the prompt. Link in post #3 now fixed

    I agree that its easier to write a NOT IN query in this case. However, as previously stated its very inefficient as it cannot make use of indexed fields
    That's why I prefer, if possible, to use an unmatched query even if I use two stacked queries to achieve the result.
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  14. #14
    Stan Denman is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    55
    Thanks so much Colin!!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Pairing data based on quantity
    By kishino in forum Modules
    Replies: 27
    Last Post: 08-26-2020, 10:50 AM
  2. Pairing records from excell
    By Jen0dorf in forum Access
    Replies: 6
    Last Post: 07-29-2015, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums