Results 1 to 6 of 6
  1. #1
    Western_Neil is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jul 2016
    Location
    Calgary, AB
    Posts
    147

    How write SQL with a double reference the same table

    Hello;
    I'm trying to get a VBA SQL statement to work. The schema I have is:
    Table 1 tblElmts ElmtID ElmtTag ElmtTitle ElmtNote
    Table 2 tblSubElmts SubElmtID UpperElmtFK ElmtQty LowerElmtFK SubElmtNote
    Relationships are 1 to many between the ID and the FKs (both)

    This SQL works:
    SELECT tblElmts.ElmtID, tblSubElmts.SubElmtID, tblSubElmts.UpperElmtFK, tblElmts.ElmtTag," _
    & " tblSubElmts.ElmtQty, tblSubElmts.LowerElmtFK, tblSubElmts.SubElmtNote" _
    & " FROM tblElmts INNER JOIN tblSubElmts ON tblElmts.ElmtID = tblSubElmts.UpperElmtFK" _


    & " WHERE UpperElmtFK=37"
    But only gives the Upper or Lower ElmtTag. depending on UpperElmtFK or LowerElmtFK use in the ON clause.
    I was trying to place an “AS tbl2up” in the JOIN, which fails, to allow placing the second one (Lower) in as a subquery. The question is how to return both ElmtTag s in the query. I do realise that ElmtID will equal UpperElmtFK , giving the same Upper ElmtTag. I would prefer not to remove this at this time.

    A 2nd question would be "Is it better make the bottom subquery the most complex and then work out, or just cut the size down the quickest?

    Thanks for Looking

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,900
    Use Access query designer. Pull tblElmts into table/query window twice and link each to tbl SubElmts. Then switch to SQLView to see the statement.
    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 MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Qu2: cut the size down the quickest so the other subquery has a smaller dataset to handle
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  4. #4
    Western_Neil is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jul 2016
    Location
    Calgary, AB
    Posts
    147
    Thanks June, but I get a Null return. Rereading it I guessing I'm not doing the linking right. How should the designer look with the right link?

  5. #5
    Western_Neil is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jul 2016
    Location
    Calgary, AB
    Posts
    147
    Thanks Colin. That I thought, smallest and then muck with it

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,900
    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.

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

Similar Threads

  1. Replies: 4
    Last Post: 02-07-2018, 02:57 PM
  2. Replies: 7
    Last Post: 03-11-2017, 11:00 AM
  3. Deleting double quotes out of my table?
    By kawi6rr in forum Queries
    Replies: 3
    Last Post: 02-19-2013, 04:32 PM
  4. ODBC table read to blank Access table write
    By dibblejon in forum Access
    Replies: 2
    Last Post: 03-10-2010, 08:39 AM
  5. Replies: 0
    Last Post: 08-01-2009, 12:43 PM

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