Results 1 to 12 of 12
  1. #1
    WCStarks is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Posts
    314

    Union Query for two queries from the same table

    I am attempting to do a Union Query to combine two sibling queries into a single query. Querying online, I found there is such a thing as a Union Query. Union Queries seem rather simple, but it is not working for me.

    I need to retrieve all Credits from the Mem_Trans table and a sub-set of Debits from the same table. I need to end up with columns for [Trans Date], Credits and Debits. When I run the Union Query, only the Credits come across. Can Union Queries not work with the same table? Here is what I have in my Union Query. Each sub-query has the same number of columns, even though the Credits query doesn't need the [Member ID]. Why does the second query not run?
    Code:
    SELECT Mem_Trans.Credit, Mem_Trans.[Trans Date], Mem_Trans.[Member ID]
    FROM Mem_Trans
    WHERE (((Mem_Trans.Credit)<>IsNull([Credit])))
    
    Union
    
    SELECT Mem_Trans.Debit, Mem_Trans.[Trans Date], Mem_Trans.[Member ID]
    FROM Mem_Trans
    WHERE (((Mem_Trans.Debit)<>IsNull([Debit])) AND ((Mem_Trans.[Member ID])=34));


  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try

    SELECT 0 As Debit, Mem_Trans.Credit, Mem_Trans.[Trans Date], Mem_Trans.[Member ID]
    FROM Mem_Trans
    WHERE Mem_Trans.Credit Is Not Null

    Union

    SELECT Mem_Trans.Debit, 0 As Credit, Mem_Trans.[Trans Date], Mem_Trans.[Member ID]
    FROM Mem_Trans
    WHERE Mem_Trans.Debit Is Not Null AND Mem_Trans.[Member ID]=34
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    WCStarks is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Posts
    314
    That works! Thanks. I wonder why MS does not talk about this when they show us how to do Union Queries?

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help! Not sure but I suppose they can't cover every use.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    WCStarks is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Posts
    314
    OK. To clarify: Place holders in each query are only needed when the two queries are drawing data from the same table. In the MS help example, the two queries were drawing data from different tables. Apparently, in this case, no place holders from the other query are needed. Does this correctly clarify the rules?

    This being the case, it seems that this is an important detail MS should have included in their explanation of using Union Queries.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No, placeholders are needed when you want to return different fields from each query (in your case one had debits and one had credits, you wanted them returned as separate fields). Normally a union query is pulling from different tables, so that's probably why their example did. I'm not even sure you need it. Does this work?

    SELECT Mem_Trans.Debit, Mem_Trans.Credit, Mem_Trans.[Trans Date], Mem_Trans.[Member ID]
    FROM Mem_Trans
    WHERE (Mem_Trans.Debit Is Not Null AND Mem_Trans.[Member ID]=34) OR Mem_Trans.Credit Is Not Null
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    WCStarks is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Posts
    314
    I tried your suggested query, since I could recall if I had not already tried that syntax. It does not work correctly. It includes all debits for all members, not just for the SCATeam "member" record.

    My criteria is to return only those debits linked to the SCATeam financial transactions, ignoring debits from the remainder of the member records. The debits for the other member records is used only as a "flag" showing what the team member owes the team. When the team member pays that amount, he gets a receipt and it gets recorded as a credit against the debit, essentially cancelling out the debit. So, all the credits need to be returned as income to the SCATeam, but only the debits belonging to the SCATeam should be returned. If not, the "flag" debits in the member records, would cancel out that portion of the credits as income to the team.

    I had tried several different ways to get the result I needed, but could not get it. I toyed with the idea of creating a temp table and appending each query to it. That seemed overly involved, requiring a macro to run a sequence of queries, so I searched online for a better solution and found the Union Query at the top of the list.

    I started the db a year ago with the single purpose of tracking financial transactions between the team and its members. It did not initially include tracking all of the team transactions. Since then, I have expanded the db to include tracking all financial transactions, membership, organization, events, assets, communication sites and EOCs for the various cities in the county. The SCATeam (a volunteer group of Amateur Radio Operators) is under the Emergency Services division of the County Sheriff's Office, like Search And Rescue, except our team manages emergency communications for the the SO.

    FYI: As you know from recent postings you have helped with, the db has multiple tabs under the Members Tab, along with the other tabs at the Members level. When I expanded what the db included, I didn't know how to have sub-tabs under forms. I found the Access feature of Navigation Subforms, which seemed to be just what I needed. Unfortunately, I hadn't yet discovered the Tab Controls, which would also have provided the form structures I needed. So I blindly developed with the Navigation Subforms, which would have been better done with Tab Controls. But now, I do not know how to migrate away from the Navigation Subfoms.

  8. #8
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    I agree with Paul.
    The single query in post #6 should be functionally the same as what you were showing with a union query in post #1.
    Perhaps you need to review the filter was applied correctly or review what you had originally
    Last edited by isladogs; 12-26-2018 at 05:33 PM.
    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

  9. #9
    WCStarks is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Posts
    314
    Well, I don't see how. I copied and pasted the above SQL into a query, adding the missing ";" and ran it, and it did not eliminate the member debits. I just now recreated the query, using the above SQL and it still includes the member debits. I guess I should indicate that the debit and credit fields are in the same transaction record. Please read my description of the use of Debit in this case.

  10. #10
    WCStarks is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Posts
    314
    Here is an image showing the first page of the result of the above single query. I needed a way to track what members owed and when, and when that amount owed was paid. As I do it now, I enter a debit to indicate an amount owned by the member, then when he pays, I update the same transaction record with the credit and date of the credit, which shows the debt has be paid. I admit this is an unusual way of using the debit field. If I were to put the debit and credit in their own transaction records, then the above single query method would likely work, but not as it is now. As I am doing it now, I can see at a glance when a particular amount owed has been paid. Perhaps I should revisit this use of debit in the future. An accountant would probably cringe.

    The transactions for the SCATeam itself, is done in the more traditional way, with separate credit and debit transactions.
    Click image for larger version. 

Name:	Debit-Credit.JPG 
Views:	16 
Size:	99.9 KB 
ID:	36717

    As it is done now, the Union query does fulfill the objective.

  11. #11
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    I'm not an accountant but I have to admit that I'm indeed 'cringing' at the way you record your credit / debit pairs.
    Also surely there are times where the entire debit isn't paid in a single payment.
    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

  12. #12
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    I thing that debits and credits should be keeped in separate tables, but,
    if you like to keep them in the same record, i propose to keep in separate field the date of their transactions.

    in addition...

    the criteria in the second query must be changed to:

    Code:
    WHERE (Mem_Trans.Debit Is Not Null AND Mem_Trans.[Member ID]=34) 
    OR (Mem_Trans.Credit Is Not Null AND Mem_Trans.[Member ID]=34)
    while the first UNION query might have the following syntax:
    Code:
    SELECT  'Credit' AS [Trans Type], Mem_Trans.Credit AS [Amount], Mem_Trans.[Trans Date], Mem_Trans.[Member ID]
    FROM Mem_Trans
    WHERE Mem_Trans.Credit Is Not Null
    
    Union
    
    SELECT 'Debit' AS [Trans Type], Mem_Trans.Debit AS [Amount], Mem_Trans.[Trans Date], Mem_Trans.[Member ID]
    FROM Mem_Trans
    WHERE Mem_Trans.Debit Is Not Null AND Mem_Trans.[Member ID]=34

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

Similar Threads

  1. SQL: Union Query to Combine 3 Queries
    By pdevito3 in forum Access
    Replies: 6
    Last Post: 06-03-2015, 12:53 PM
  2. Union Query or Multiple Queries.???
    By MFS in forum Queries
    Replies: 9
    Last Post: 08-30-2014, 04:48 AM
  3. Union query to join two crosstab queries
    By racefan91 in forum Queries
    Replies: 5
    Last Post: 09-26-2013, 10:24 AM
  4. Union query- 9 Queries
    By JessieBee in forum Queries
    Replies: 3
    Last Post: 07-14-2012, 05:46 AM
  5. One filter for 3 Queries in Union Query
    By rlsublime in forum Access
    Replies: 3
    Last Post: 03-20-2012, 04:01 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