+ Reply to Thread
Results 1 to 12 of 12

MS Query - query to return value based on two joins within the same tables

  1. #1
    Registered User
    Join Date
    12-09-2009
    Location
    NY USA
    MS-Off Ver
    Excel 2016 and 2010
    Posts
    27

    Question MS Query - query to return value based on two joins within the same tables

    Hello All.

    I'm trying to create a Microsoft Query on Excel that connects to an external database and was having a SQL question on how to return query records based on information below.

    Although it's in excel, I believe this is more of a general SQL question. I apologize and thank you for the long read in advance!

    Below are 2 Table structures that only include fields that are relevant to this question.

    *==========*
    | Transaction |
    *==========*
    TransactionID (Unique)
    EntityID
    ForEntityID

    *=======*
    | Entity |
    *=======*
    EntityID (Unique)
    Name

    Using the example table structures from above, I currently have a microsoft query which returns all records from [Transaction] Table and also join the [Entity] Table by (Entity.EntityID = Transaction.EntityID) so that it returns on record row the name of the Entity as one of the columns.

    I am having trouble understanding how I may be able to achieve the next task.

    The values of 'Transaction.EntityID' and 'Transaction.ForEntityID' are both consisting of the IDs that match the Unique ID of 'Entity.EntityID'.

    While most records in [Transaction] Table have empty values for 'Transaction.ForEntityID', Some records do contain values in both 'EntityID' and 'ForEntityID'

    For those records in [Transaction] where there is value in 'Transaction.ForEntityID', I want to append another column in the query that shows the name of the entity that the record is being entered "For" (the value in 'Entity.Name').

    The problem however, is that I already have a join on (Entity.EntityID = Transaction.EntityID). It wouldn't be logically possible to add another join (Entity.EntityID = Transaction.ForEntityID) is this correct?

    Simply put, what I'm hoping to achieve is:

    If there's a value in 'Transaction.ForEntityID', I want to append a column in the query that returns the matching value from Entity Table (Entity.EntityID = Transaction.ForEntityID) but if there is no value in 'Transaction.ForEntityID', I'd like that query column to return the matching value from Entity Table (Entity.EntityID = Transaction.EntityID)

    For Example:

    *=================*
    | Transaction Records |
    *=================*
    TransactionID - EntityID - ForEntityID
    100 - 1 - (blank)
    101 - 1 - 2
    102 - 2 - (blank)

    *============*
    | Entity Records |
    *============*
    EntityID - Name
    1 - Albert
    2 - Brian

    On the query, How can it be structured so that the query record are returned as follows :
    ID: 100, EntityID: 1, ForEntityID: , EntityName: Albert
    ID: 101, EntityID: 1, ForEntityID: 2, EntityName: Brian
    ID: 102, EntityID: 2, ForEntityID: , EntityName: Brian

    I'm sorry for not the most efficient way of asking the question due to my limited knowledge in this matter. Hope you understand, and I appreciate any help!

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,873

    Re: MS Query - query to return value based on two joins within the same tables

    When I create the tables in an Access Data Base and create the following Query, I get the expected results. You should be able to take this SQL statement and adapt it to your MS Query

    PHP Code: 
    SELECT Transaction.TrnasctionIDTransaction.EntityIDTransaction.ForEntityID, [Entity Records].Namex
    FROM 
    [TransactionINNER JOIN [Entity RecordsON Transaction.EntityID = [Entity Records].EntityID
    In Access the word "Name" is a reserved name and should not be used in a field name. Instead in this case, I used Namex. Be sure to change in your SQL statement.
    Last edited by alansidman; 12-14-2016 at 02:37 PM.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Valued Forum Contributor kasan's Avatar
    Join Date
    07-22-2009
    Location
    Riga, Latvia
    MS-Off Ver
    Excel 2010
    Posts
    680

    Re: MS Query - query to return value based on two joins within the same tables

    Hi,
    I think I got your need, will try to explain my idea.
    First part of code (before Union) will return all records where Entity.EntityID = Trans.EntityID AND Trans.ForEntityID is empty.
    Result should be:
    ID: 100, EntityID: 1, ForEntityID: , EntityName: Albert
    ID: 102, EntityID: 2, ForEntityID: , EntityName: Brian

    Union means that we want to add something to first return. Using Union - same columns might be selected, but criteria can be different.
    So now we will select exactly the same columns, BUT note that now Trans.ForEntityID = Entity.EntityID and we are interested only in records where Trans.ForEntityID is not empty.
    Result should be:
    ID: 101, EntityID: 1, ForEntityID: 2, EntityName: Brian

    Final output will combine those two selection in one table.
    In Microsoft Query find button "SQL" and paste there code you see below, but be sure that all column names and table names are valid.

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    12-09-2009
    Location
    NY USA
    MS-Off Ver
    Excel 2016 and 2010
    Posts
    27

    Re: MS Query - query to return value based on two joins within the same tables

    Thank you so much alansidman and kasan!

    Kasan's suggestion with UNION had gotten me very close to what I was looking to achieve and I thank you greatly for that suggestion. This new query however is still returning less rows than before, so I'm currently in the midst of trying to figure out why those missing records are being left out.

    Thanks! again and will report back with any findings.

  5. #5
    Valued Forum Contributor kasan's Avatar
    Join Date
    07-22-2009
    Location
    Riga, Latvia
    MS-Off Ver
    Excel 2010
    Posts
    680

    Re: MS Query - query to return value based on two joins within the same tables

    Thanks for feedback.
    Result is less rows than expected? Union will not return duplicate rows, if you need all records - use UNION ALL.
    And may be you could give us small sample of your data + your query code?

  6. #6
    Registered User
    Join Date
    12-09-2009
    Location
    NY USA
    MS-Off Ver
    Excel 2016 and 2010
    Posts
    27

    Re: MS Query - query to return value based on two joins within the same tables

    So the issue for missing rows (and I believe this issue goes a bit outside the scope of the original inquiry) was that by having "WHERE Entity.EntityID = Trans.EntityID" or "WHERE Entity.ForEntityID = Trans.EntityID" , it was not returning the records from the Trans table where Trans.ID was blank or ''.

    Now, I tried modifying

    WHERE Entity.EntityID = Trans.EntityID AND another criteria AND another criteria

    to

    WHERE (Entity.EntityID = Trans.EntityID OR Trans.EntityID = '') AND another criteria AND another criteria

    but now it's returning all rows from both tables.

  7. #7
    Registered User
    Join Date
    12-14-2016
    Location
    New Orleans
    MS-Off Ver
    2016
    Posts
    3

    Re: MS Query - query to return value based on two joins within the same tables

    Thanks for help, I tried to solve it for some months...

  8. #8
    Registered User
    Join Date
    12-09-2009
    Location
    NY USA
    MS-Off Ver
    Excel 2016 and 2010
    Posts
    27

    Re: MS Query - query to return value based on two joins within the same tables

    Quote Originally Posted by kasan View Post
    Thanks for feedback.
    Result is less rows than expected? Union will not return duplicate rows, if you need all records - use UNION ALL.
    And may be you could give us small sample of your data + your query code?
    Yes, to be clear, it's not UNION that's causing the missing rows and believe I need to modify the WHERE section so that it includes rows where all rows from transaction table are listed, not just the ones where Trans.EntityID = Entity.EntityID

    I can post the actual query but wondering if I should post a new topic as it may be outside the scope of original question.
    Please Login or Register  to view this content.
    The red highlight is the section in question from the real example. The above sql is also not including union (as I discovered this missing records issue after I posted the original question) and I need to work on the WHERE aspect to be able to also retrieve records where GLTrans.ID = '' even before I can attach a union.
    Last edited by smk224; 12-14-2016 at 04:27 PM.

  9. #9
    Valued Forum Contributor kasan's Avatar
    Join Date
    07-22-2009
    Location
    Riga, Latvia
    MS-Off Ver
    Excel 2010
    Posts
    680

    Re: MS Query - query to return value based on two joins within the same tables

    " it was not returning the records from the Trans table where Trans.ID was blank or '' "
    " WHERE (Entity.EntityID = Trans.EntityID OR Trans.EntityID = '') AND another criteria AND another criteria "
    Does this mean that Trans.EntityID can be blank in your table? Like:
    *=================*
    | Transaction Records |
    *=================*
    TransactionID - EntityID - ForEntityID
    100 1 (blank)
    101 1 2
    102 2 (blank)
    103 (blank) (blank)

  10. #10
    Registered User
    Join Date
    12-09-2009
    Location
    NY USA
    MS-Off Ver
    Excel 2016 and 2010
    Posts
    27

    Re: MS Query - query to return value based on two joins within the same tables

    Quote Originally Posted by kasan View Post
    " it was not returning the records from the Trans table where Trans.ID was blank or '' "
    " WHERE (Entity.EntityID = Trans.EntityID OR Trans.EntityID = '') AND another criteria AND another criteria "
    Does this mean that Trans.EntityID can be blank in your table? Like:
    *=================*
    | Transaction Records |
    *=================*
    TransactionID - EntityID - ForEntityID
    100 1 (blank)
    101 1 2
    102 2 (blank)
    103 (blank) (blank)
    Yes sir.

    But still would like that record TransactionID=103 to be retrieved in the query.

  11. #11
    Valued Forum Contributor kasan's Avatar
    Join Date
    07-22-2009
    Location
    Riga, Latvia
    MS-Off Ver
    Excel 2010
    Posts
    680

    Re: MS Query - query to return value based on two joins within the same tables

    to post #8
    How about:

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    12-09-2009
    Location
    NY USA
    MS-Off Ver
    Excel 2016 and 2010
    Posts
    27

    Re: MS Query - query to return value based on two joins within the same tables

    Quote Originally Posted by kasan View Post
    to post #8
    How about:

    Please Login or Register  to view this content.
    Ohh.. so i need to connect 2 queries with union (and with that added to the original inquiry), it looks like I have to connect 4 queries with union? I'll give that a try and report back. Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Return values in batch via custom web query based upon cell value?
    By freddiejson82 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-11-2015, 08:41 AM
  2. Web Query Macro return phone number and website based on data recieved
    By sclog in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-23-2015, 06:10 PM
  3. Replies: 13
    Last Post: 09-16-2014, 04:21 PM
  4. Query criteria based of other query data
    By jik_ff in forum Access Tables & Databases
    Replies: 6
    Last Post: 03-12-2012, 05:26 PM
  5. Excel VBA query time based hyperlink query
    By TAU710 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-13-2010, 03:27 PM
  6. Replies: 8
    Last Post: 08-06-2010, 07:27 AM
  7. Web Query Doesn't Return All Tables With Excel 2002
    By Bill in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-22-2005, 12:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1