Home > Mobile >  MS access query group dates to start date and end date range
MS access query group dates to start date and end date range

Time:10-25

I have table for permission

ID UserID Date Permission
1 1370 2022/10/24 Approved
2 1370 2022/10/31 Rejected
3 1370 2022/11/07 Approved
4 1370 2022/11/14 Approved
5 1370 2022/11/21 Rejected
6 1370 2022/11/28 Rejected
7 1370 2022/12/05 Rejected
8 1370 2022/12/12 Approved
9 1370 2022/12/19 Approved
10 1370 2022/12/26 Approved
11 1370 2023/01/02 Rejected
12 2456 2022/12/26 Rejected
13 2456 2023/01/02 Rejected
14 2456 2023/01/09 Approved
15 2456 2023/01/16 Approved

I want select query to group dates based on start date and end date

result query like this:

UserID Date From Date Till Permission
1370 2022/10/24 2022/10/24 Approved
1370 2022/10/31 2022/10/31 Rejected
1370 2022/11/07 2022/11/14 Approved
1370 2022/11/21 2022/12/05 Rejected
1370 2022/12/12 2022/12/26 Approved
1370 2023/01/02 2023/01/02 Rejected
2456 2022/12/26 2023/01/02 Rejected
2456 2023/01/09 2023/01/16 Approved

EDIT:

this is a part of my table for userID = 1370 and 2456. users request for permission and system approve or reject their request. each day can reject or approved. so when i want to response their request , either get them result of all 365 days of year separately , or group rows with start and end date period. –

CodePudding user response:

I solve it by user Defined(udf) function in vba.

Function partID(userID, rowID)
Dim dbs As DAO.Database
Dim rst As DAO.Recordset

    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset("select * from permission where userID = " & userID & " order by permission.date")
    
    i = 0
    p = ""
    
    Do While rst.EOF = False
        If p <> rst("permission") Then
            i = i   1
            p = rst("permission")
        End If
        If rowID = rst("ID") Then
            Exit Do
        End If
        rst.MoveNext
    Loop
    partID = i

End Function

then select query and group it.

SELECT UserID, Min(Date) AS DateFrom, Max(Date) AS DateTill, Permission
FROM Permission
GROUP BY UserID, Permission, partid([userid],[id])
ORDER BY UserID, partid([userid],[id]);

and it works... :D

CodePudding user response:

It's a little unclear what corolation there is between these date? (Relationship) im guessing that these are related somehow ie a Job number of sorts??? (U need to clarify) Ie ID 3 4 in the 1st Tbl??? And im guessing that if there is a Relationship you would need to show/add that to your Qry IE Table with dates, another with Job ref And also, im guessing, if there is a from date and a null till date then Till date = From date?

  • Related