Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Data Bender's Avatar
    Data Bender is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Aug 2013
    Posts
    10

    Access 2007 - Blank column from JOIN and UNION queries.

    Greetings! This is my very first question asked on a such a forum. I'm excited to get much needed feedback from such brilliant people as yourselves.
    I'm trying to query (in Access 2007) multiple monthly tables, all JOIN'ed to the below GeoIndex table as follows:

    Click image for larger version. 

Name:	AccessForum1.png 
Views:	6 
Size:	6.1 KB 
ID:	13561

    The desired result set (or an acceptable alternative) would look like the following:
    Click image for larger version. 

Name:	AccessForum2.png 
Views:	6 
Size:	11.6 KB 
ID:	13562

    I have tried queries that first JOIN then UNION, and queries that first UNION then JOIN. However, I always end up with an empty AugEmpID column. Otherwise, the result sets look fine.

    I plan to PivotTable MonthlyRoster in Excel, for regular headcount reports.
    Please advise. I look forward to your brilliant feedback.

  2. #2
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    Your db is nor normalized for your monthly data. Your table for employees should be

    Roster
    ------------
    CostCenter
    EmplID
    Month

    then in your query it would look like this:

    Code:
    SELECT GeoIndex.CostCenter, GeoIndex.Region, IIf([rMonth]="Jul",[EmplID],Null) AS JulEmplID, IIf([rMonth]="Aug",[EmplID],Null) AS AugEmplID
    FROM GeoIndex INNER JOIN Roster ON GeoIndex.CostCenter = Roster.CostCenter
    ORDER BY IIf([rMonth]="Jul",[EmplID],Null), IIf([rMonth]="Aug",[EmplID],Null);
    Attached Files Attached Files

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    A UNION will effectively structure the data as it should have been to begin, as Alan has noted.

    The UNION would be

    SELECT CostCenter, EmpID, "Jul" As M FROM JulRoster
    UNION SELECT CostCenter, EmpID, "Aug" FROM AugRoster
    .... ;

    Then you can join that query to GeoIndex, or you can include the GeoIndex in the UNION by joining in each SELECT line.

    Then you can do a CROSSTAB. Why export to Excel?
    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.

  4. #4
    Data Bender's Avatar
    Data Bender is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Aug 2013
    Posts
    10
    Thanks for your feedback, alansidman. Per your recommendation, I added "Month" columns to the source data tables to normalize them. Then, I literally copied and pasted your above code, changing the table and column titles as needed. When I ran it the first time, both JulEmpID and AugEmpID columns were blank. So, I removed the "r" from [rMonth], which gave me JulEmpID but no AugEmpID... and now I'm back where I started. Does your above code assume that one table contains all months? Because each month actually has its own table.

    Again, thank you for trying to help this Access noob. Your kindness is much appreciated.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    Alan's suggestion is to normalize the data, this means ONE table with ONE Month column, not 12 columns. Did you read my post #3?
    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.

  6. #6
    Data Bender's Avatar
    Data Bender is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Aug 2013
    Posts
    10
    Hi, June7. Thank you for your feedback. I took your advice, and ran the following:

    Code:
    SELECT CostCenter, EmpID, "Jul" As M 
    FROM JulRoster
    UNION SELECT CostCenter, EmpID, "Aug"
    FROM AugRoster;
    ...which resulted as follows (note: I'm actually running Jun and Jul numbers right now, and will add Aug later):

    Click image for larger version. 

Name:	June7_1.png 
Views:	5 
Size:	48.1 KB 
ID:	13578

    So far, so good.

    Then, I ran the following JOIN query:
    Code:
    SELECT GeoIndex.*, June7.EmpID, June7.M
    FROM June7
    RIGHT JOIN GeoIndex
    ON GeoIndex.CostCenter= June7.CostCenter;
    And, got this again:
    Click image for larger version. 

Name:	June7_2.png 
Views:	5 
Size:	50.5 KB 
ID:	13579
    The total records show me that the correct records are there. Yet, for some reason, the result set won't yield the EmpID's. Any ideas? Anyone? Please?

    And, to answer your question, I'm exporting to Excel because that's my wheelhouse (not Access, as you may have guessed by now haha).

    I look forward to your reply.

  7. #7
    Data Bender's Avatar
    Data Bender is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Aug 2013
    Posts
    10
    Thanks, June7, for the clarification of Alan's post.

    I did read your post. Thank you for that too.

    This is the next best thing to having actual mentors in my office. You all are wonderful!

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    Are the EmpID fields in each table employing a Lookup? I never set Lookups in tables.

    Still don't understand why export to Excel if all this data is in Access tables.
    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.

  9. #9
    Data Bender's Avatar
    Data Bender is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Aug 2013
    Posts
    10
    The EmpID fields are text, with no Lookups.

    For you, June7, (and myself) I will learn to generate reports directly from Access. In the meantime, my boss' timeframes require my use of Excel, as I am faster with that application.

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    Then I can't explain the blank field. Would have to examine database
    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.

  11. #11
    Data Bender's Avatar
    Data Bender is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Aug 2013
    Posts
    10
    That's a total bummer.

    I'm positive my employer would not approve of my sharing the database, even in this instance.

    Thank you anyway, June7.

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    It is a weird issue. Even if you were using Lookups, something should show in the field. You could remove sensitive data. Follow instructions at bottom of my post.
    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
    Data Bender's Avatar
    Data Bender is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Aug 2013
    Posts
    10

    Lightbulb Success!

    SUCCESS!

    In short, I stopped thinking like a spreadsheet and started thinking like a database (which is what I believe alansidman was trying to tell me to do).

    First, I cleaned up some column names on the source tables, by changing "CC" and "DeptNo" to "CostCenter". (Perhaps that was unneccesary, but made my SQL code much easier to read for this noob.)

    Then, I normalized the output, rather than insisted on a spreadsheet-like result set, which looks like the following:

    UNION_Query
    ----------------
    CostCenter
    EmpID
    EOMDate

    Here's the code:
    Code:
    SELECT CostCenter, EmpID, EOMDate
    FROM JunRoster
    UNION SELECT CostCenter, EmpID, EOMDate
    FROM JulRoster
    UNION SELECT CostCenter, EmpID, EOMDate
    FROM AugRoster;
    Finally, I RIGHT JOIN'ed UNION_Query to the GeoIndex, which yielded the following normalized table:

    JOIN_Query
    --------------
    CostCenter
    Region
    EmpID
    EOMDate

    Code:
    SELECT GeoIndex.*, UNION_Query.EmpID, UNION_Query.EOMDate
    FROM UNION_Query
    RIGHT JOIN GeoIndex
    ON UNION_Query.CostCenter = GeoIndex.CostCenter
    ORDER BY GeoIndex.CostCenter;
    I can now PivotTable the normalized result set in Excel. Yay! (I know... I know, June7. I can do the same in Access with a crosstab. I'll get there. )

    In any case, both June7 and alansidman were helpful by confirming that my method and code were sound. I merely needed to change the parameters.

    I love this forum, and look forward to giving back!

  14. #14
    Data Bender's Avatar
    Data Bender is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Aug 2013
    Posts
    10
    Nutz... I wanted to add to both alan's and June7's reputations. However, I'm only allowed to do so once on this thread.

  15. #15
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    I rep'd June for you.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 09-06-2012, 06:04 AM
  2. Replies: 2
    Last Post: 12-22-2011, 07:53 PM
  3. Unequal Join or Union?
    By cap.zadi in forum Queries
    Replies: 3
    Last Post: 09-23-2011, 05:57 AM
  4. Union query (or Inner/Outer join?)
    By LilMissAttack in forum Queries
    Replies: 4
    Last Post: 10-23-2010, 12:36 AM
  5. Replies: 3
    Last Post: 05-21-2010, 03:57 PM

Tags for this Thread

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