Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / New Users / December 2007

Tip: Looking for answers? Try searching our database.

Attendance Query

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
alexcuse - 18 Dec 2007 01:17 GMT
I want to generate a query that calculates the work hours of all
employees based on adding/subtracting from an 80 hour work period.

TABLES (3):

EMPLOYEE:
EmpNum    FirstName    LastName
1        John        Smith
2        Jane        Doe

CALENDAR:
Year    PayPeriod    BeginDate    EndDate
2007        1    1/1/2007    1/14/2007
2007        2    1/15/2007    1/28/2007

ATTENDANCE:
EmpNum    Date        Code    Hours
1                 1/2/2007    A    1
1        1/3/2007    T    0.5
2        1/3/2007    S    8

Code "A" means extra hours worked, "T" means Tardy, "S" means Sick.

I thought about designating "Hours" as either positive or negative,
but I think this could be very easily prone to errors during data
entry.

I want my query to display:

EmpNum    FirstName    LastName    Hours
1        John        Smith        80.5 (from 80+1-0.5)
2        Jane        Doe        72 (from 80-8)

Here is what I have for the query

SELECT E.EmpNum, E.FirstName, E.LastName,
 80-SUM(A1.Hours)-(SUM(A1.Minutes)/60)+SUM(A2.Hours)+(SUM(A2.Minutes)/
60) AS Hours

FROM Employee AS E, Calendar AS C, Attendance AS A1, Attendance AS A2

WHERE C.Year=[Insert Year] AND C.PayPeriod=[Insert PayPeriod]
  AND
         (E.EmpNum=A1.EmpNum AND A1.Date BETWEEN C.BeginDate AND
C.EndDate
          AND A1.AttendanceCode<>'A')
  OR
         (E.EmpNum=A2.EmpNum AND A2.Date BETWEEN C.BeginDate AND
C.EndDate
          AND A2.AttendanceCode='A')

GROUP BY E.EmpNum, E.FirstName, E.LastName;

But it is giving me problems, where the hours for John Smith and Jane
Done seems to get added together.  What I am doing wrong?

Thanks a lot for your help!
Alex
Dale Fye - 18 Dec 2007 17:55 GMT
Alex,

Untested, but this should work.  Notice that I have replaced your [Date] and
[Year] fields with AttDate and CalYear.  These (Date, Year) are both reserved
words in Access and should not be used as field names.

SELECT E.EmpNum, E.FirstName, E.LastName, 80+A1.Delta
FROM Employee E
LEFT JOIN            
(SELECT A.EmpNum, SUM(IIF([Code] = "A", 1, -1) * [Hours]) as Delta
FROM Attendance A INNER JOIN Calendar C
ON A.AttDate BETWEEN C.BeginDate AND C.EndDate
WHERE C.CalYear=[Insert Year] AND C.PayPeriod=[Insert PayPeriod]
GROUP BY A.EmpNum) as A1
ON E.EmpNum = A1.EmpNum

The subquery joins the Attendance and Calendar tables and then filters for
only the year and payperiod values you enter when you run the query.

HTH
Dale

Signature

Don''t forget to rate the post if it was helpful!

email address is invalid
Please reply to newsgroup only.

> I want to generate a query that calculates the work hours of all
> employees based on adding/subtracting from an 80 hour work period.
[quoted text clipped - 54 lines]
> Thanks a lot for your help!
> Alex
alexcuse - 18 Dec 2007 21:09 GMT
Thanks for all your help Dale!  It worked perfectly, and it even
included names that from the Employee table that wasn't in the
Attendance table (which was what I wanted).  I will definitely try to
learn from your usage of IIF and JOIN with inner selects in designing
other queries.  Thanks also for the syntax correction regarding Date
and Year, I will check the other tables to make sure they don't have
bad naming.

Thanks a lot,
-Alex

> Alex,
>
[quoted text clipped - 82 lines]
> > Thanks a lot for your help!
> > Alex
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.