mercredi 31 août 2016

MS access if condition doesn't work

I used to have one query that does a bunch of things, one of them is tell me about the salary increase for each employee. Is it due? is it overdue? is it too early for one?

The query kept asking me to enter parameters and after asking this question removing "enter parameter value" in query MS access I broke the one query into a bunch, each query builds on the other until the last one that has the if condition. Everything worked exactly the way I want it to, except for the if condition (that used to work fine before!!!)

This is my if condition

Eligibility: IIf([MonthsSinceLastIncrease]<24,IIf([MonthsSinceLastIncrease]>=18 And [LastOfRatings]<=2,"OVERDUE",IIf([MonthsSinceLastIncrease]>=15 And [LastOfRatings]=1,"OVERDUE",IIf([MonthsSinceLastIncrease]>=9 And [MonthsSinceLastIncrease]<15 And [LastOfRatings]=1,"Eligible",IIf([MonthsSinceLastIncrease]>=12 And [MonthsSinceLastIncrease]<18 And [LastOfRatings]=2,"Eligible","ok")))),"OVERDUE")

The rule for salary increases are as follows:

  • If the employee's rating is 3, he gets an increase after 24 months of his last one
  • If the employee's rating is 2, he gets one in 12-18 months
  • If the employee's rating is 1, he gets one in 9-15 months

What happens now, is that for some of the employees, I get only one record and it's correct, but for some employees, I get two records, one of them is correct an the other is not. Now instead of 48 records (what I used to get when the condition worked fine), I get 58.

This is the code for the whole query

SELECT IIf([MonthsSinceLastIncrease]<24,IIf([MonthsSinceLastIncrease]>=18 And [LastOfRatings]<=2,"OVERDUE",IIf([MonthsSinceLastIncrease]>=15 And [LastOfRatings]=1,"OVERDUE",IIf([MonthsSinceLastIncrease]>=9 And [MonthsSinceLastIncrease]<15 And [LastOfRatings]=1,"Eligible",IIf([MonthsSinceLastIncrease]>=12 And [MonthsSinceLastIncrease]<18 And [LastOfRatings]=2,"Eligible","ok")))),"OVERDUE") AS Eligibility, MonthsSinceLastUpdateQ.LocalID, MonthsSinceLastUpdateQ.LastOfRatings, MonthsSinceLastUpdateQ.MaxOfDateOfUpdate, MonthsSinceLastUpdateQ.MonthsSinceLastIncrease
FROM MonthsSinceLastUpdateQ, DateOfUpdateQ
GROUP BY IIf([MonthsSinceLastIncrease]<24,IIf([MonthsSinceLastIncrease]>=18 And [LastOfRatings]<=2,"OVERDUE",IIf([MonthsSinceLastIncrease]>=15 And [LastOfRatings]=1,"OVERDUE",IIf([MonthsSinceLastIncrease]>=9 And [MonthsSinceLastIncrease]<15 And [LastOfRatings]=1,"Eligible",IIf([MonthsSinceLastIncrease]>=12 And [MonthsSinceLastIncrease]<18 And [LastOfRatings]=2,"Eligible","ok")))),"OVERDUE"), MonthsSinceLastUpdateQ.LocalID, MonthsSinceLastUpdateQ.LastOfRatings, MonthsSinceLastUpdateQ.MaxOfDateOfUpdate, MonthsSinceLastUpdateQ.MonthsSinceLastIncrease;

Your help is much appreciated, please note that I barely know coding and have only been learning MS Access as I go, thank you!

Aucun commentaire:

Enregistrer un commentaire