Results 1 to 9 of 9
  1. #1
    Fuii is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2020
    Posts
    12

    Generate From-To list with grouping columns

    Hola,

    from an internal source about employees I'm trying to fetch a list about the employees team assignments.
    For every date the source got the employee reference and the team assignment reference.

    To use that for other queries I need to kinda aggregate it from a daily list, into a from-to list; grouped by the employee and team reference.



    The source-picture (picture3) has the structure of the data source. For every single day (days can miss if OFF/PTO) the assignment per employee.
    The qry-picture (picture2) shows my puny attempt. If the same team is being switched multiple times it ignores the in-between changes and causing overlapping from-to results (see picture1).

    The intendedresult-picture (picture4) is what I ultimately need.
    Data by employee + team with the respective start & end time that does not have overlapping entries.

    Any hints appreciated.

    Happy new year btw!
    Attached Thumbnails Attached Thumbnails WrongResult.JPG   Qry.JPG   Source.JPG   IntendedResult.JPG  

  2. #2
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,474
    Doesn't look like your table has a primary key like an autonumber value/field? If not you should add it.

    Thinking maybe nested queries.
    Query1 = Fields (we_id and team_id) Group on both to get unique records.
    Query2 = all 3 fields, group on we_id and team_id and do Min on date_full,
    Query3 = all 3 fields, group on we_id and team_id and do Max on date_full
    Query4 = include all 3 queries, Link Query1, Query2, Query3 on we_id and team_id and display (we_id and team_id from Query1 and MinDate_Full from Query2 and MaxDate_full from Query 3.
    At least thats how I see it quickly in my head

  3. #3
    Fuii is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2020
    Posts
    12
    Thanks Bulzie.

    I skipped qry1 as the source actually has a combined pKey of wd_id + date_full; so only 1 line per day+employee can exist.
    Though the result in the end is the same as my attempt; the team_id 22 would show as 1 line item (after removing duplicates from the result) from 01/01 to 01/07 instead of 2 line items with 01/01 to 01/02 + 01/06 to 01/07 :/

    Also attached the sample access file this time to make it easier.
    Attached Thumbnails Attached Thumbnails Bulzie4.JPG  
    Attached Files Attached Files

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I suspect Access SQL doesn't have resources to do this alone and VBA will be needed.

    Problem is distinguishing the two team 22 assignment periods and considering them separately. If records had assignment numbers, this could be fairly simple. First 2 team 22 records would be assignment 1 and last 2 would be assignment 3. A VBA procedure could populate field with this value.

    Code:
    Sub Assignment()
    Dim rs As DAO.Recordset, x As Long, sWD As String, sT As Integer
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM source ORDER BY wd_id, date_full")
    sWD = rs!wd_id
    sT = rs!team_id
    x = 1
    Do While Not rs.EOF
        If sWD <> rs!wd_id Then
            x = 0
            sWD = rs!wd_id
        End If
        If sT <> rs!team_id Then
            x = x + 1
            sT = rs!team_id
        End If
        rs.Edit
        rs!AssignNum = x
        rs.Update
        rs.MoveNext
    Loop
    End Sub
    Then query to get min and max dates for each assignment.

    SELECT source.wd_id, source.team_id, source.AssignNum, Min(source.date_full) AS sDate, Max(source.date_full) AS eDate
    FROM source
    GROUP BY source.wd_id, source.team_id, source.AssignNum
    ORDER BY source.AssignNum;

    wd_id team_id AssignNum sDate eDate
    1003423 22 1 1/1/2024 1/2/2024
    1003423 17 2 1/3/2024 1/5/2024
    1003423 22 3 1/6/2024 1/7/2024
    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.

  5. #5
    Fuii is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2020
    Posts
    12
    I dont mind using VBA; its in use a lot already.

    However, to make your solution work June7 I would now also need a script that adds that AssignNum values either to the archive (source table) or on the fly as interim output to make the Assignment() script work - speaking of 100k+ records here from thousands of peeps.
    Any suggestion for that?

    I can't change the original report source (3rd party); only adding stuff as of my local version.

  6. #6
    Fuii is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2020
    Posts
    12
    Scratch that, misunderstood it; the VBA script is to update the source and then run the SQL code on it.
    That works fine, thanks - also tested with a 2nd wd_id to validate that the script keeps enumerate properly.

    Though, that now leads to another issue.

    If a single day does not appear in the source, the output is skipping that day too.
    On first glance its correct, but I'll need to have the "prior assignment" be applied to that day (as part of the from-to range).

    Here in that result the source does not have data for the 2nd Jan.

    Click image for larger version. 

Name:	NewIssueSource.JPG 
Views:	23 
Size:	28.8 KB 
ID:	51301

    Thus the output skips that day too.
    Click image for larger version. 

Name:	NewIssueQuery.JPG 
Views:	23 
Size:	28.3 KB 
ID:	51302

    However, I will need that the prior entry that only lasts until Jan 1st is extended to the 2nd in that case (or whatever the last "missing date" before the next entry is), like:
    Click image for larger version. 

Name:	NewIssueIntended.JPG 
Views:	23 
Size:	28.8 KB 
ID:	51303

    Updated sample file attached.

    That probably can't be done with the same script, but another one checking for gaps (DateDif > 1) with index -1 of current row?
    Attached Files Attached Files

  7. #7
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    for your edate problem, try this sql

    Code:
    SELECT Q1.wd_id, Q1.team_id, Q1.AssignNum, Q1.sDate, [edat]-([edat]=[sdate]) AS eDate
    FROM (SELECT source.wd_id, source.team_id, source.AssignNum, Min(source.date_full) AS sDate, Max(source.date_full) AS eDat
    FROM source
    GROUP BY source.wd_id, source.team_id, source.AssignNum
    ORDER BY source.AssignNum)  AS Q1;
    edit: this solution is based on your description. I did not understand what 'or whatever the last "missing date" before the next entry is' means. What about the last entry where there is no later date?

    or on the fly as interim output
    you might want to take a look at the solution in this thread which on the face of it is very similar to yours in generating an assign num value

    https://www.accessforums.net/showthread.php?t=89214

    the function will need some modification to meet your specific requirements for incrementing

    This way you might find better to get your dates in order before generating your assign num value
    Last edited by CJ_London; 01-04-2024 at 06:35 AM. Reason: clarification

  8. #8
    madpiet is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    154
    Are you just trying to do something simple like exploding a (startDate, endDate) pair to all the dates in the range? If that's the case scrap all this VBA glop and just create a calendar table.

    then you can just outer/cross join with the calendar table and filter the dates down for each record.
    SELECT <other stuff>, c.TheDate
    FROM <other stuff> o INNER JOIN Calendar c ON (c.TheDate >= o.StartDate AND c.TheDate<=o.EndDate)

    and you get one record per date for each <other stuff> record. No VBA required. I have nothing against VBA, but I'm not sure it's necessary here. And it doesn't scale at all.

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    madpiet, there is no enddate field in table. It must be obtained from a subsequent record. And this process is complicated by nature of data and what OP wants to output. Actually, goal is to compact records, not explode.
    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. Grouping based on three columns - Snaking
    By AnneForumer in forum Queries
    Replies: 14
    Last Post: 10-07-2016, 01:43 PM
  2. Grouping Columns with dates
    By gauravnagpal in forum Access
    Replies: 6
    Last Post: 04-10-2012, 06:03 AM
  3. Grouping and Transapose rows with columns
    By HendriX99 in forum Queries
    Replies: 3
    Last Post: 03-14-2011, 03:23 PM
  4. Generate datasheet columns on the fly?
    By kman42 in forum Programming
    Replies: 0
    Last Post: 03-11-2011, 03:52 AM
  5. Replies: 1
    Last Post: 10-01-2009, 06:41 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