How to create a query to calculate leave balance based on type (1 Viewer)

Jenklot

New member
Local time
Tomorrow, 02:49
Joined
Feb 16, 2009
Messages
9
[FONT=&quot]Could someone please help me to solve this query? How can I create a query that’s enabled me to check leave balance of each/ all type of leave al & el (same leave category), mc & cl at the end of every week/ month based on staff id? The Excel file is currently used for entering leave application & checking.[/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot]Thanks[/FONT]
 

Attachments

  • Leave Application.zip
    73.1 KB · Views: 1,341

jal

Registered User.
Local time
Today, 11:49
Joined
Mar 30, 2007
Messages
1,709
You have codes:
AL
MC
EL
Which leave type does each stand for?
 

Jenklot

New member
Local time
Tomorrow, 02:49
Joined
Feb 16, 2009
Messages
9
You have codes:
AL
MC
EL
Which leave type does each stand for?

Jal,

Ok, AL stands for Annual Leave, MC for Medical Leave & Emergency leave for EL. AL & EL are under the same category, meaning that taking EL will decrease total leave balance.
 

jal

Registered User.
Local time
Today, 11:49
Joined
Mar 30, 2007
Messages
1,709
There is also compassionate leave. What's the code for that?

How many of these codes decrease total leave balance? So far you said that only AL and EL do so. So Compassionate Leave isn't used? Medical Leave isn't used?

Or, if they are all used in the calculation, why then did you say that only AL & EL are in the same category? Aren't they all in the same category if all are used?

Maybe you need to give us a simple formula indicating what sort of calculation you want.
 

Jenklot

New member
Local time
Tomorrow, 02:49
Joined
Feb 16, 2009
Messages
9
There is 3 types of leave as listed below where Emergency leave (Non Approved Leave Application) is under the same category with Annual leave (Approved Leave) and Medical & Compassionate stands on its own.

No
Leave Type 1 Annual/ Emergency Leave 2 Medical Leave 3 Compassionate Leave
Every employees is entitle at least 18 days of Annual Leave, 28 days of Medical Leave & 10 days Compassionate Leave for a year. Lets say I took
1 day of Annual Leave for each month plus a total of 4 days of Emergency Leave & 7 days total of Medical Leave so by the end of the year, the balance would be 2 days (18 - 12 - 4)for Annual Leave & 21 days for Medical Leave.

Hope that's help....
 

jal

Registered User.
Local time
Today, 11:49
Joined
Mar 30, 2007
Messages
1,709
- Why do you have some null StaffIDs? I deleted those records to avoid distractions.
- WARNING: I rarely get a query right on the first try. When I'm helping someone, I don't do the testing. It's YOUR RESPONSIBILITY to test it IN EVERY POSSIBLE WAY and then let me know if there is a problem. Under no circumstances should you assume that I got it right. Ok, here we go.

- Here is a base query. Paste it into SQL View and save it under the name qryLeaveForm. It's just the totals from table LeaveForm and it groups EL and AL together.
SELECT staff_ID, IIF(leave_Type = 'EL', 'AL', leave_Type) as leaveType, Sum(Total_Days) as DaysTaken FROM [LEAVE FORM]
GROUP BY staff_ID, IIF(leave_Type = 'EL', 'AL', leave_Type)

I make use of the above query (qryLeaveForm) - I use it like a table - in the query below. I do three subtractions, as you can see below (I put a blank line in between each so you can see what I'm doing).
(1) entitlement_Leave. I substraced the 'AL' + 'EL' total.
(2) compassionate_leave. I subtracted the 'CL' total
(3) mc_leave. I subtracted the 'MC' total.
As you can see I assumed there are four codes
AL
EL
CL
MC
And here is the query:

SELECT Staff_ID,
entitlement_leave - NZ((SELECT QLF.DaysTaken FROM qryLeaveForm as QLF WHERE QLF.Staff_ID = PLD.Staff_ID AND QLF.leaveType = 'AL'),0) as AnnualLeave,

compassionate_leave - NZ((SELECT QLF.DaysTaken FROM qryLeaveForm as QLF WHERE QLF.Staff_ID = PLD.Staff_ID AND QLF.leaveType = 'CL'),0) as CompassionateLeave,

mc_leave - NZ((SELECT QLF.DaysTaken FROM qryLeaveForm as QLF WHERE QLF.Staff_ID = PLD.Staff_ID AND QLF.leaveType = 'MC'),0) as MedicalLeave

FROM [Personal Leave Data] as PLD



Here I use the keyword NZ to subtract zero as to avoid subtracting 'null' - nulls exist in cases where the employee hasn't yet used any of his days.

I did nothing year-specific. Instead I made things easier on myself by assuming that all the data in the tables would be for the current year.
And I did nothing with "leave balance from last year."
 

jal

Registered User.
Local time
Today, 11:49
Joined
Mar 30, 2007
Messages
1,709
I realized later that I could have done the same thing just using a single query. Again, it's three subtractions:

SELECT Staff_ID,
entitlement_leave - NZ((SELECT SUM(LF.Total_Days) FROM [Leave Form] as LF WHERE LF.Staff_ID = PLD.Staff_ID AND LF.Leave_Type IN ('AL', 'EL')),0) as AnnualLeave,

compassionate_leave - NZ((SELECT Sum(LF.Total_Days) FROM [Leave Form] as LF WHERE LF.Staff_ID = PLD.Staff_ID AND LF.Leave_Type = 'CL'),0) as CompassionateLeave,

mc_leave - NZ((SELECT SUM(LF.Total_Days) FROM [Leave Form] as LF WHERE LF.Staff_ID = PLD.Staff_ID AND LF.Leave_Type = 'MC'),0) as MedicalLeave

FROM [Personal Leave Data] as PLD
 

Jenklot

New member
Local time
Tomorrow, 02:49
Joined
Feb 16, 2009
Messages
9
Those who got "leave balance" from last year will get their total leave wrong, example a person with a balance of 2 days will get their answer wrong by 2.

last year balance = 2 days
this year leave entitlement = 18 days
total accumulate leave = 20 days

al & el taken so far = 10 days

it should be 10 days left but stated 8 days.

Other than that everything seems ok.

Thanks again
 
Last edited:

Jenklot

New member
Local time
Tomorrow, 02:49
Joined
Feb 16, 2009
Messages
9
I just add [leave_balance] in front and everything seems right

[leave_balance] + [entitlement_leave] - NZ((SELECT QLF.DaysTaken FROM qryLeaveForm as QLF WHERE QLF.Staff_ID = PLD.Staff_ID AND QLF.leaveType = 'AL'),0) as AnnualLeave,

Will post result after more testing done
 

jal

Registered User.
Local time
Today, 11:49
Joined
Mar 30, 2007
Messages
1,709
Good for you. Again, if you use Version 2, I think you will only need one query (you won't need qryLeaveForm).
 

Jenklot

New member
Local time
Tomorrow, 02:49
Joined
Feb 16, 2009
Messages
9
Thanks, you really help me out big time, by the way is it ok if I PMed you for future reference or any questions regarding Access in particular?
 

jal

Registered User.
Local time
Today, 11:49
Joined
Mar 30, 2007
Messages
1,709
I'd prefer you post any new questions to new threads. Besides - believe me - there are several people on this forum who know ten times as much about accesss as I do. I usually bide my time in the "Modules and VBA" forum because learning VBA helps me learn VB6 as well.
 

Jenklot

New member
Local time
Tomorrow, 02:49
Joined
Feb 16, 2009
Messages
9
Could you please explain on the expression that you used...
NZ((SELECT SUM(LF.Total_Days) FROM [Leave Form] as LF WHERE LF.Staff_ID = PLD.Staff_ID AND LF.Leave_Type IN ('AL', 'EL')),0) as AnnualLeave,

I'm a bit confuse on the usage of "nz", "select sum" and "in"
 

jal

Registered User.
Local time
Today, 11:49
Joined
Mar 30, 2007
Messages
1,709
The IN keyword substitutes for the OR keyword:

WHERE ID = 4 OR ID = 5 OR ID = 6

This could be shortened to:

WHERE ID IN(4, 5, 6)

The NZ keyword is used to supply an alternate value (a default value) in cases where a non-existent value (a null value) might cause a problem.

SELECT LastName, NZ(AccountNumber, 'No Account Number') FROM Customers

The above reports the last name and account number for each customer. However, for those customers who do not have an account number, it reports 'No Account Number'. So the result might look like this:

LastName....AccountNumber
Smith.........343433
Johnson.......1234228
Morrison......No Account Number
 

jal

Registered User.
Local time
Today, 11:49
Joined
Mar 30, 2007
Messages
1,709
The SUM keyword does just what it says. It totals the values in a column.
 

Users who are viewing this thread

Top Bottom