Query 3 tables and populate records

raghuram.star

Board Regular
Joined
Sep 5, 2012
Messages
102
Can you please help me query 3 tables and return the records.

Table 1 : Employee Data
Code:
Emp No  Name  Dept
123       Raghu  Fin
124       Ram    Hr
125       Cris   Ops
126       Giri   IT

Table 2 : Activity Data
Code:
Emp No    Name  Date     Hours
123       Raghu  1/2/2015   5
123       Raghu  1/3/2015   6
123       Raghu  1/4/2015   4
124       Ram    1/2/2015   5
124       Ram    1/3/2015   6
125       Cris   1/2/2015   5
125       Cris   1/3/2015   6
125       Cris   1/4/2015   2

Table 3 : Work Days
Code:
Date
1/2/2015
1/3/2015
1/4/2015

Relation between tables
Table 1 : Table 2
[Emp No]

Table 2 : Table 3
[Date]

I would love for a query to populate employe records who did not work in working days or having less work hours (less than 5 hours).

Final Table: Work Log
Code:
Emp No    Name   Dept  Date    Hours
123       Raghu   Fin  1/4/2015   4
124       Ram     Hr   1/4/2015   0
125       Cris    Ops  1/4/2015   2
126       Giri    IT   1/2/2015   0
126       Giri    IT   1/3/2015   0
126       Giri    IT   1/4/2015   0

Thank you very much in advance. I'm excited to hear what is possible!

Ram
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Here is query code that will return what you want:
Code:
SELECT X.*, Nz([Activity Data].Hours,0)+0 as Hours
FROM 
(SELECT [Emp No], Name, Date
 FROM [Employee Data], [Work Days])  AS X
LEFT OUTER JOIN [Activity Data]
ON X.[Emp No]=[Activity Data].[Emp No]
AND X.[Date]=[Activity Data].[Date]
WHERE (Nz([Activity Data].Hours,0)+0) < 5;
A few database design notes/tips:

1. You should not have the Name field in both the Employee Data and Activity Data tables. It should only Appear in the Employee Data table. If you are looking at the Activity Data table and want to see what the Employees name is, you can find that by linking to the Employee Data table on the Emp No field.

2. You should not used reserved words like "Name" and "Date" as field names. This ambiguity can lead to errors and unexpected results (Access may have trouble deciphering if you are referencing a field name you created or a method, property, function).
 
Upvote 0

Forum statistics

Threads
1,221,875
Messages
6,162,563
Members
451,775
Latest member
Aiden Jenner

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top