473,386 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Access nested select with left join question

My end result is a report of multiple employees that includes days worked in a date range. In my report, I need to have all dates in the range showing on the report whether the employee worked it or not.

So I have a table with dates worked and hours for each empl. I also created a table with dates far into the future. I think I need a query that joins the two tables and puts all dates for the date range - that will be prompted. added to the table of employee time worked.

Does anyone have any ideas how to join these tables and get all dates for each employee in the two week range that will be prompted?

First table name Dates has only 1 column with dates way into the future.

Second table name TotHoursQuery (which is acutally a query) has Empl Name, Date worked, and hours worked.

The code I have so far works sort of, but prompts for empl_name and cal_date (for some reason) - and the expected end date, but no start date. If I put nothing in the prompts except put date in end date, it comes back with the original data that is in TotHoursQuery and adds the empl_name and cal_date fields to the view, with nothing in them. My code below..

Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM (SELECT DISTINCT Nested.empl_name, Nested.cal_date 
  3. FROM Dates, TotHoursQuery 
  4.       WHERE Nested.cal_date between [start date] and [end date]) AS Nested 
  5. LEFT JOIN TotHoursQuery 
  6.       ON (Nested.cal_date = TotHoursQuery.[Start Date]) 
  7.       AND (Nested.empl_name = TotHoursQuery.[Full Name]);
Apr 3 '17 #1

✓ answered by NeoPa

Oh. You want a full set of dates for each employee, where each employee has data covering a whole set of dates itself. That's a whole different thing from what I understood first time round.

First of all, let's introduce you to the Between construct as used in WHERE clauses.
Expand|Select|Wrap|Line Numbers
  1. WHERE [DateField] <= X AND [DateField] >= Y
is equivalent to :
Expand|Select|Wrap|Line Numbers
  1. WHERE [DateField] Between X And Y
Now, your requirement means you need a set of data on the LEFT side of your JOIN which contains a set of dates for each employee. Once you have that you can follow the same principle as before. LEFT JOIN that source of data, that is probably going to be two tables linked together by an INNER JOIN and tied together using parentheses, to the data containing your employee dates (probably [TotHoursQuery]) and you should have what you require.

5 1121
NeoPa
32,556 Expert Mod 16PB
Hi Shawn.

I think you need to get the basics right first before trying to move on.

When you have a set of dates in one table that reflect your date range, plus more at each end possibly, then you start with a simple query filtered (WHERE clause) by the date range. If you want to add to that the data from another table that may or may not overlap with your selected dates then you add it as a LEFT JOIN to the date table.

Your existing attempt seems to employ the cartesian approach. I see no reason you'd want that, but maybe I don't understand your situation clearly enough.
Apr 4 '17 #2
Thanks for the reply. I started over with the code below which almost works. It only brings back record of dates that are not used by any employee. What I need is for each employee, I need all dates in the range. Even if the employee did not work that date. So basically, I need the equivalent of a For loop in a sql statement. This is for a time sheet that will be printed for each employee.
Expand|Select|Wrap|Line Numbers
  1. SELECT a.Field1, b.[Start Date], b.[Full Name], b.[Employee ID], b.[Location], b.[Sum Of Duration]
  2. FROM Dates AS a LEFT JOIN TotHoursQuery AS b ON b.[Start Date] = a.Field1
  3. WHERE a.Field1 >= [Enter Start Date: - yyyy-mm-dd] AND  a.[Field1] <= [Enter End Date: - yyyy-mm-dd]
  4. ORDER BY b.[Full Name];
Apr 4 '17 #3
NeoPa
32,556 Expert Mod 16PB
Oh. You want a full set of dates for each employee, where each employee has data covering a whole set of dates itself. That's a whole different thing from what I understood first time round.

First of all, let's introduce you to the Between construct as used in WHERE clauses.
Expand|Select|Wrap|Line Numbers
  1. WHERE [DateField] <= X AND [DateField] >= Y
is equivalent to :
Expand|Select|Wrap|Line Numbers
  1. WHERE [DateField] Between X And Y
Now, your requirement means you need a set of data on the LEFT side of your JOIN which contains a set of dates for each employee. Once you have that you can follow the same principle as before. LEFT JOIN that source of data, that is probably going to be two tables linked together by an INNER JOIN and tied together using parentheses, to the data containing your employee dates (probably [TotHoursQuery]) and you should have what you require.
Apr 5 '17 #4
NeoPa, thank you so much! It took me a little while, but I now have what I need.
Apr 5 '17 #5
NeoPa
32,556 Expert Mod 16PB
Pleased to help Shawn :-)
Apr 6 '17 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Colman | last post by:
Howdy all! I guess I'm a newbie, because I am stumped (or maybe just too durned tired). Here's what I got... CREATE TABLE `nodecat_map` ( `nodecat_id` mediumint(8) unsigned NOT NULL...
1
by: Paul Bramscher | last post by:
Here's one for pathological SQL programmers. I've got a table of things called elements. They're components, sort of like amino acids, which come together to form complex web pages -- as nodes...
4
by: jbm05 | last post by:
Hi, I'm curious about the computational complexity of a query I have. The query contains multiple nested self left joins, starting with a simple select, then doing a self left join with the...
3
by: Allan | last post by:
Please help, below is my problem. Let's say I have 2 tables, a Products table and a Colors table that go as follow: Table Products prodID Name 1 shirt 2 tshirt
9
by: Alan Lane | last post by:
Hello world: Background: Yesterday, January 21, Doug Steele was kind enough to help me out on a Left Join problem. I was trying to return all stores and their Gross Adds for December, 2004...
14
by: Ryan | last post by:
I want to do the following SQL statement in Access. However, it won't allow me to have the secondary part of my join statement and tells me that this is not supported. OK, so Access doesn't support...
0
by: tricard | last post by:
Good day all, I have four tables that I am attempting to LEFT JOIN together in a query: tblPartNumber (PartNumberID is primary key autonumber) tblPartNumberVendor (PartNumberVendorID is primary...
9
by: P3Eddie | last post by:
Hello all! I don't know if this can even be done, but I'm sure you will either help or suggest another avenue to accomplish the same. My problem may be a simple find duplicates / do something...
3
by: rrstudio2 | last post by:
If I have two tables and need to do a left outer join and include a where statement on the second table, it seems like the left outer join becomes an inner join. For example: Table: Names id...
5
by: jimatqsi | last post by:
I have some code that works fine in an all Access environment. I am moving the back-end to SQL Server 2012. After moving the back-end I am getting this error when I run a particular query. ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.