Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am new to SQL but I think I'm nearly there with this one. I have an ms access database with a table called SicknessLog. The fields are ID, StaffName, [Start/Return], Date.

When a member of staff is off work for sickness then a record is added to the table and the value in the [Start/Return] field is 1. When they return to work a new record is added with the same details except the [Start/Return] field is 0.

I am trying to write a query that shows the number of days between when a member of staff went sick ([Start/Return] value of -1) and when they returned to work ([Start/Return] value of 0)

So far I have this

SELECT [staffname], DATEDIFF("d", min([date]), max([date])) as [Days Sick]
FROM sicknesslog
WHERE [date] > (now()-30)
GROUP BY [staffname]

This shows how many days off each staff member has had in the last 30 days. The only problem I have is that it is also listing 0 days for staff who are currently away. If possible I would like it to show the number of days since they went sick up intil now for staff who have not returned.

Thanks in advance for any help
Posted
Updated 14-Jun-14 1:19am
v3
Comments
Peter Leow 14-Jun-14 7:55am    
What if a staff went for 2 sick leaves in a month, say from 1 June 2014 to 3 June 2014, and then again from 11 June 2014 to 12 June 2014, your query will output the result of a total of 11 days, is that what you want?
Member 10884049 14-Jun-14 8:09am    
Hi, thanks for reply. Yes I would want it to count total days off for all instances of sickness within that month. I just noticed another problem with my query. It will not work for staff who returned from sickness in last month but left earlier than that. Trying to figure out how to re-write it. I've had no sql training so learning as I go but it's very tricky

1 solution

Substtute now() for max([date] when min and max are equal :

IIF(min([date])=max([date]),now(),max([date]))


Cheers
 
Share this answer
 
Comments
Member 10884049 14-Jun-14 8:14am    
Hi Estys. Thanks for reply. I tried your code as below

SELECT [staffname], DATEDIFF("d",min([date]),max([date])) AS [Days Sick]
FROM sicknesslog
WHERE [date]>(IIF(min([date])=max([date]),now(),max([date]))-30)
GROUP BY [staffname];

But I am getting error message "Cannot have aggregate function in Where clause"?
Member 10884049 14-Jun-14 8:27am    
Oh wait, I think you meant like this? SELECT [staffname], DATEDIFF("d",min([date]),IIF(min([date])=max([date]),now(),max([date]))) AS [Days Sick]
FROM sicknesslog
WHERE [date]>(now()-30)
GROUP BY [staffname];
Member 10884049 14-Jun-14 8:29am    
Now I just need to account for instances where staff returned from sickness within last 30 days but had left before then. (they will have one entry but this needs to be ignored)
Estys 14-Jun-14 9:53am    
You also need to consider the situation that someone has been absent in more than one period the last 30 days.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900