简体   繁体   中英

Is there a way to do this using an SQL query in MS Access?

Basically, I want to know how many days an employee is an active subordinate for a given supervisor on a given month.

The sample data comes from a single table with a few columns:

EmployeeName    Previous_Supervisor Updated_Supervisor  Updated_Status  ChangeType  EffectiveDate
john    peter   james       Supervisor  16-Sep-20
john    james   jude        Supervisor  24-Sep-20
john    jude    jude    Resigned    Status  30-Sep-20

All columns are just short text, except for EffectiveDate

And here's what I want to achieve through a query (ActiveDayCount column):

EmployeeName    Previous_Supervisor Updated_Supervisor  Updated_Status  ChangeType  EffectiveDate   ActiveDayCount
john    peter   james       Supervisor  16-Sep-20   15
john    james   jude        Supervisor  24-Sep-20   8
john    jude    jude    Resigned    Status  30-Sep-20   6

ActiveDayCount column:

First row's ActiveDayCount is 15 since the effective date is Sep 16, 2020, so that counted Sept 1-15.
This means for 15 days, john was Peter's(Previous_Supervisor) subordinate.
    Second row's ActiveDayCount is 8 since the effective date is Sep 24, 2020, so that counted Sept 16-23.
This means for 8 days, john was james'(Previous_Supervisor) subordinate.
    Third row's ActiveDayCount is 6 since the effective date is Sep 30, 2020, so that counted Sept 24-29.
This means for 6 days, john was jude's(Previous_Supervisor) subordinate before he resigned.

EDIT: Here's a simple query I have to get the first table:

SELECT EmployeeName, Previous_Supervisor, Updated_Supervisor, Updated_Status, ChangeType, EffectiveDate
FROM RosterHistory

Can this be done in MS access using SQL queries?

I believe this gives something close to what you've asked for. (Whether it's what you want or need may be a different matter as there are assumptions built into your question, such as that all your dates fall within a given month and hence if there is no prior record then the days are counted from the beginning of the month).

SELECT EmployeeName, 
       Previous_Supervisor, 
       Updated_Supervisor, 
       ChangeType, 
       Updated_Status, 
       EffectiveDate, 
       (SELECT MAX(EffectiveDate) FROM RosterHistory AS rh
             WHERE EmployeeName = RosterHistory.EmployeeName 
             AND EffectiveDate < RosterHistory.EffectiveDate) AS DatePrevious, 
        IIF(ISNULL(DatePrevious),
            DAY(EffectiveDate) - 1, 
            EffectiveDate - DatePrevious) AS ActiveDayCount
FROM RosterHistory;

Note: Although it looks innocuous, the "AS rh" in RosterHistory AS rh above is critically important. Without that the query will run but will give incorrect output/no values for the DatePrevious column.

Just to note, I have tested this in MS Access 2016 with a copy of the sample data provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM