Results 1 to 7 of 7
  1. #1
    tylerg11 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    200

    Group by Query

    I have a question regarding Group By queries. I'm grouping on a specific table to show the most recent records (so for 5 different quotes for the same project it will show one record - the most recent one). I can do this without a problem.



    However, what if I also want to show some fields from a related table with a 1-to-1 relationship with my table? Does this need to be two separate queries? Do I need to do the grouping query first? Just wondering if I could do it by grouping on some fields but not others but that doesn't look possible.

  2. #2
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    1) You can join the tables before you do the GROUP BY, and then use the First() function to request Access to ggive you the desired values from any arbitrary record in the group.

    2) You can do the GROUP BY and then join the grouped record to your other table, as long as all the fields needed for the join are present on the grouped records.

  3. #3
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Supposing you had a table of time logged, and a staff table.
    Code:
    tblStaff
       StaffID
       StaffName
       
    tblLog
       LogID
       StaffID
       LogInDate
       LogOutDate
       HoursLogged
    Now, this gives you the hours in the last 30 days, but you also want the staff name off the staff table:
    Code:
    qryLog:
    SELECT 
       TL.StaffID,
       Max(TL.LoginDate) As LastLoggedIn,
       SUM(TL.HoursLogged) AS HoursInLast30Days
    FROM
       tblLog AS TL
    WHERE 
       TL.LoginDate > Date() - 30
    GROUP BY 
       TL.StaffID;
    You could join the records before you do the sum, and then accept the first name on any record for a given staffid:
    Code:
    SELECT 
       TL.StaffID,
       First(TS.StaffName),
       Max(TL.LoginDate) As LastLoggedIn,
       SUM(TL.HoursLogged) AS HoursInLast30Days
    FROM
       tblLog AS TL
       INNER JOIN
       tblStaff AS TS
       ON TL.StaffID = TS.StaffID
    WHERE 
       TL.LoginDate > Date() - 30
    GROUP BY 
       TS.StaffID;
    or you could join the results of qrylog to the staff table and get the same result, probably slightly more efficiently:
    Code:
    SELECT 
       QL.StaffID,
       TS.StaffName,
       QL.LastLoggedIn,
       QL.HoursInLast30Days
    FROM
       qryLog AS QL
       INNER JOIN
       tblStaff AS TS
       ON TL.StaffID = TS.StaffID;
    you could also do it in the same query, although Access sometimes gets picky about exact syntax.
    Code:
    SELECT
       QL.StaffID,
       TS.StaffName,
       QL.LastLoggedIn,
       QL.HoursInLast30Days
    FROM 
       tblStaff AS TS
       INNER JOIN
       (SELECT 
          TL.StaffID,
          Max(TL.LoginDate) As LastLoggedIn,
          SUM(TL.HoursLogged) AS HoursInLast30Days
       FROM
          tblLog AS TL
       WHERE 
          TL.LoginDate > Date() - 30
       GROUP BY 
          TL.StaffID) AS QL
       ON TS.StaffID = QL.StaffID;

  4. #4
    tylerg11 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    200
    Quote Originally Posted by Dal Jeanis View Post
    1) You can join the tables before you do the GROUP BY, and then use the First() function to request Access to ggive you the desired values from any arbitrary record in the group.
    Yes, this is really what I'm trying to do. Join tables and get a clean set of records and then group from there. Here's my problem. Let's say I have 5 quotes for the same project and I want to only show the most recent quote for the project. One of the fields is "Status" which is different for all 5 quotes. This means when I group (which forces me to group on all fields) I will get multiple quotes to show when I only want one. See what I'm trying to do here? Thanks.

  5. #5
    tylerg11 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    200
    Basically I'm trying to GROUP before JOIN if that makes any sense. Done through the Access query interface GROUP occurs after the join.

  6. #6
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    The third example shows how that should look in SQL.

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    And another possible solution http://allenbrowne.com/subquery-01.html#TopN
    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: 07-24-2013, 02:34 PM
  2. Replies: 0
    Last Post: 02-25-2013, 04:43 PM
  3. Replies: 2
    Last Post: 06-21-2012, 07:40 PM
  4. query group by over another group by?
    By gap in forum Queries
    Replies: 2
    Last Post: 07-04-2011, 12:59 AM
  5. columns for group detail but not group header?
    By Coolpapabell in forum Reports
    Replies: 0
    Last Post: 08-21-2009, 08:53 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