473,499 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query One table with Multiple Date Ranges

8 New Member
OK This is my 1st post to any forum, but I rely on forums all the time. Short story is I find myself needing to become a Access DBA for work.

Situation: I want to use one table of events that Track (Employee ID, Employee Name, Event Date and Trip Hours) And be able to add the fields(Week #1, Week #2, Week#3, week #4 and Week #5). *****There is also an Autonumbered (Trip Numebr).

What I have tried: I can run this as a query (Employee ID, Employee Name, Event Date and Trip Hours). I can setup various date ranges and it runs great, but that is only with one date range. Multiple ranges return a null query.

This is one of my attempts: Week #1:IIf([Event Date],Between #3/9/2008# And #3/15/2008#, sum[Total Hours],”0”)

Problem: What is the best way to summarize the [Total Hours] based on weekly date ranges that reference the [Event Date]?


Thank you very much,
-Alex
Apr 12 '08 #1
16 7222
whitbacon
8 New Member
In the query use format to as follow:

Format([eventdate], "w")

then group by on the employee id and the above column and sum on the hours field
Apr 13 '08 #2
ADezii
8,834 Recognized Expert Expert
OK This is my 1st post to any forum, but I rely on forums all the time. Short story is I find myself needing to become a Access DBA for work.

Situation: I want to use one table of events that Track (Employee ID, Employee Name, Event Date and Trip Hours) And be able to add the fields(Week #1, Week #2, Week#3, week #4 and Week #5). *****There is also an Autonumbered (Trip Numebr).

What I have tried: I can run this as a query (Employee ID, Employee Name, Event Date and Trip Hours). I can setup various date ranges and it runs great, but that is only with one date range. Multiple ranges return a null query.

This is one of my attempts: Week #1:IIf([Event Date],Between #3/9/2008# And #3/15/2008#, sum[Total Hours],”0”)

Problem: What is the best way to summarize the [Total Hours] based on weekly date ranges that reference the [Event Date]?


Thank you very much,
-Alex
Assuming you are filtered for a specific/current Year, I'll post some sample code and leave the rest up to you (I allowed for January to March 1). This will probably provide you with more than you need, but you can easily eliminate what you don't want. I've assumed your Table Name is Table1.
Expand|Select|Wrap|Line Numbers
  1. SELECT CInt(Format$([Event Date],"ww")) AS [Week Number], fConvertWeekRange([Event Date]) AS [Week Range], 
  2. Sum(Table1.[Trip Hours]) AS [SumOfTrip Hours]
  3. FROM Table1
  4. GROUP BY CInt(Format$([Event Date],"ww")), fConvertWeekRange([Event Date]);
Expand|Select|Wrap|Line Numbers
  1. Public Function fConvertWeekRange(dteEventDate As Date) As String
  2. Dim intWeekNumber As Integer
  3.  
  4. intWeekNumber = CInt(Format$(dteEventDate, "ww"))
  5.  
  6. Select Case intWeekNumber
  7.   Case 1
  8.     fConvertWeekRange = "Jan. 1, 2008 to Jan. 5, 2008"
  9.   Case 2
  10.     fConvertWeekRange = "Jan. 6, 2008 to Jan. 12, 2008"
  11.   Case 3
  12.     fConvertWeekRange = "Jan. 13, 2008 to Jan. 19, 2008"
  13.   Case 4
  14.     fConvertWeekRange = "Jan. 20, 2008 to Jan. 26, 2008"
  15.   Case 5
  16.     fConvertWeekRange = "Jan. 27, 2008 to Feb. 2, 2008"
  17.   Case 6
  18.     fConvertWeekRange = "Feb. 3, 2008 to Feb. 9, 2008"
  19.   Case 7
  20.     fConvertWeekRange = "Feb. 10, 2008 to Feb 16, 2008"
  21.   Case 8
  22.     fConvertWeekRange = "Feb. 17, 2008 to Feb. 23, 2008"
  23.   Case 9
  24.     fConvertWeekRange = "Feb. 24, 2008 to Mar. 1, 2008"
  25.   'etc...
  26.   Case 10
  27.   Case Else
  28.     fConvertWeekRange = ""
  29. End Select
  30. End Function
Sample OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Week Number    Week Range                        Total Hours for Range
  2. 1            Jan. 1, 2008 to Jan. 5, 2008               100
  3. 2            Jan. 6, 2008 to Jan. 12, 2008               143
  4. 3            Jan. 13, 2008 to Jan. 19, 2008                 7
  5. 4            Jan. 20, 2008 to Jan. 26, 2008                56
  6. 5            Jan. 27, 2008 to Feb. 2, 2008               100
  7. 6            Feb. 3, 2008 to Feb. 9, 2008                53
  8. 7            Feb. 10, 2008 to Feb 16, 2008                61
  9. 8            Feb. 17, 2008 to Feb. 23, 2008               146
  10. 9            Feb. 24, 2008 to Mar. 1, 2008               153
Apr 13 '08 #3
Alex30093
8 New Member
In the query use format to as follow:

Format([eventdate], "w")

then group by on the employee id and the above column and sum on the hours field
--------------------------------------------------------------------------------------------

I tried that before and then double checked it based on your suggestion. However, that just puts them in sequence without summarizing the hours. So for one employee, over four weeks I may have as high as 22 rows of data in sequence. I can use four summarized rows ( one for each week). Or in a perfect situation I would like to have:

One entry (Row) have all of the employee data (Employee ID, Employee Name) then with 4 additional (Columns) as Week numbers 1-4. That way at a glance I can see how those 22 events were distributed within those 4 weeks.

This is all part of a payroll function: This part is the data that I can import from another program. The other parts are manual. Once I get this part then I can work on reporting the manual data into a simular table of just totals
Apr 13 '08 #4
Alex30093
8 New Member
Assuming you are filtered for a specific/current Year, I'll post some sample code and leave the rest up to you (I allowed for January to March 1). This will probably provide you with more than you need, but you can easily eliminate what you don't want. I've assumed your Table Name is Table1.
Expand|Select|Wrap|Line Numbers
  1. SELECT CInt(Format$([Event Date],"ww")) AS [Week Number], fConvertWeekRange([Event Date]) AS [Week Range], 
  2. Sum(Table1.[Trip Hours]) AS [SumOfTrip Hours]
  3. FROM Table1
  4. GROUP BY CInt(Format$([Event Date],"ww")), fConvertWeekRange([Event Date]);
Expand|Select|Wrap|Line Numbers
  1. Public Function fConvertWeekRange(dteEventDate As Date) As String
  2. Dim intWeekNumber As Integer
  3.  
  4. intWeekNumber = CInt(Format$(dteEventDate, "ww"))
  5.  
  6. Select Case intWeekNumber
  7.   Case 1
  8.     fConvertWeekRange = "Jan. 1, 2008 to Jan. 5, 2008"
  9.   Case 2
  10.     fConvertWeekRange = "Jan. 6, 2008 to Jan. 12, 2008"
  11.   Case 3
  12.     fConvertWeekRange = "Jan. 13, 2008 to Jan. 19, 2008"
  13.   Case 4
  14.     fConvertWeekRange = "Jan. 20, 2008 to Jan. 26, 2008"
  15.   Case 5
  16.     fConvertWeekRange = "Jan. 27, 2008 to Feb. 2, 2008"
  17.   Case 6
  18.     fConvertWeekRange = "Feb. 3, 2008 to Feb. 9, 2008"
  19.   Case 7
  20.     fConvertWeekRange = "Feb. 10, 2008 to Feb 16, 2008"
  21.   Case 8
  22.     fConvertWeekRange = "Feb. 17, 2008 to Feb. 23, 2008"
  23.   Case 9
  24.     fConvertWeekRange = "Feb. 24, 2008 to Mar. 1, 2008"
  25.   'etc...
  26.   Case 10
  27.   Case Else
  28.     fConvertWeekRange = ""
  29. End Select
  30. End Function
Sample OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Week Number    Week Range                        Total Hours for Range
  2. 1            Jan. 1, 2008 to Jan. 5, 2008               100
  3. 2            Jan. 6, 2008 to Jan. 12, 2008               143
  4. 3            Jan. 13, 2008 to Jan. 19, 2008                 7
  5. 4            Jan. 20, 2008 to Jan. 26, 2008                56
  6. 5            Jan. 27, 2008 to Feb. 2, 2008               100
  7. 6            Feb. 3, 2008 to Feb. 9, 2008                53
  8. 7            Feb. 10, 2008 to Feb 16, 2008                61
  9. 8            Feb. 17, 2008 to Feb. 23, 2008               146
  10. 9            Feb. 24, 2008 to Mar. 1, 2008               153

This was not exactly what I was looking for but it will be extremely helpful as we look at monthly trends.

Thank you very much,
-Alex
Apr 13 '08 #5
ADezii
8,834 Recognized Expert Expert
--------------------------------------------------------------------------------------------

I tried that before and then double checked it based on your suggestion. However, that just puts them in sequence without summarizing the hours. So for one employee, over four weeks I may have as high as 22 rows of data in sequence. I can use four summarized rows ( one for each week). Or in a perfect situation I would like to have:

One entry (Row) have all of the employee data (Employee ID, Employee Name) then with 4 additional (Columns) as Week numbers 1-4. That way at a glance I can see how those 22 events were distributed within those 4 weeks.

This is all part of a payroll function: This part is the data that I can import from another program. The other parts are manual. Once I get this part then I can work on reporting the manual data into a simular table of just totals
Try this alternate approach, and let me know what you think:
Expand|Select|Wrap|Line Numbers
  1. SELECT CInt(Format$([Event Date],"ww")) AS [Week Number], fConvertWeekRange([Event Date]) AS [Week Range], 
  2. Sum(Table1.[Trip Hours]) AS [SumOfTrip Hours]
  3. FROM Table1
  4. WHERE ((Year([Event Date])=2008))
  5. GROUP BY CInt(Format$([Event Date],"ww")), fConvertWeekRange([Event Date]);
Apr 13 '08 #6
ADezii
8,834 Recognized Expert Expert
How about a Crosstab Query approach, the following Crosstab Query will summarize the Trip Hours for each EmployeeID/Employee by breaking the Event Dates into Quarters. Modify the Pivot Section to change the Date structure:
Expand|Select|Wrap|Line Numbers
  1. TRANSFORM Sum(Table1.[Trip Hours]) AS [SumOfTrip Hours]
  2. SELECT Table1.EmployeeID, Table1.[Employee Name], Sum(Table1.[Trip Hours]) AS [Total Of Trip Hours]
  3. FROM Table1
  4. GROUP BY Table1.EmployeeID, Table1.[Employee Name]
  5. PIVOT "Qtr# " & Format([Event Date],"q");
Apr 13 '08 #7
Alex30093
8 New Member
Try this alternate approach, and let me know what you think:
Expand|Select|Wrap|Line Numbers
  1. SELECT CInt(Format$([Event Date],"ww")) AS [Week Number], fConvertWeekRange([Event Date]) AS [Week Range], 
  2. Sum(Table1.[Trip Hours]) AS [SumOfTrip Hours]
  3. FROM Table1
  4. WHERE ((Year([Event Date])=2008))
  5. GROUP BY CInt(Format$([Event Date],"ww")), fConvertWeekRange([Event Date]);

I am a week into playing with Access so I don't understand a lot of this. But I put it into SQL view and did what you suggested. It says "undefined function 'fConvertWeekRange' in expression".

In previous attempts I put the dates in a weekly "ww" format. Thinking that similar weeks could be easily grouped and combined. Do I need to define a date range somewhere?
Apr 13 '08 #8
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi. If you review post #3 above you will see that ADezii posted code for you that included the skeleton of the custom function fConvertWeekRange he wrote as part of his suggested solution on your behalf. Either you have not added that function to a public code module (the named modules which show in the Module window of the database), or you have added it to a private code module (such as the code module for a form).

I would suggest that you need to consider what has already been provided to you by ADezii before loooking at other alternatives...

-Stewart
I am a week into playing with Access so I don't understand a lot of this. But I put it into SQL view and did what you suggested. It says "undefined function 'fConvertWeekRange' in expression"...
Apr 13 '08 #9
ADezii
8,834 Recognized Expert Expert
I am a week into playing with Access so I don't understand a lot of this. But I put it into SQL view and did what you suggested. It says "undefined function 'fConvertWeekRange' in expression".

In previous attempts I put the dates in a weekly "ww" format. Thinking that similar weeks could be easily grouped and combined. Do I need to define a date range somewhere?
But I put it into SQL view and did what you suggested. It says "undefined function 'fConvertWeekRange' in expression".
The Function must be declared as Public in a Standard, not Form, Code Module.

In previous attempts I put the dates in a weekly "ww" format. Thinking that similar weeks could be easily grouped and combined. Do I need to define a date range somewhere?
"ww" used in conjunction with the Format() Function will return a numeric value indicating the Week Number within the Year (1 to 52). If all you need is this along with the Summary Totals for each Week, then the solution is quite simple. This is not, however, what you requested.
Apr 13 '08 #10
Alex30093
8 New Member
Hi. If you review post #3 above you will see that ADezii posted code for you that included the skeleton of the custom function fConvertWeekRange he wrote as part of his suggested solution on your behalf. Either you have not added that function to a public code module (the named modules which show in the Module window of the database), or you have added it to a private code module (such as the code module for a form).

I would suggest that you need to consider what has already been provided to you by ADezii before loooking at other alternatives...

-Stewart

Stewart & ADezii,

Thank you, as you point it out ADezii's post does contain a way to create the ranges I am looking for. I am just so new to any form of code. I am a network specialist who just got the DBA responsibilities handed to me. So I am totally out of my area of strenght.

In looking at the answer with the "private code module" I just have no clue where to put that. I just got the Access for Dummies book so I can develope the basic knowledge to be able to impliment these suggestions.

ADezeii I am sorry your advice just went over my head.

I will study some more and see how to work with what you all have given me.


Thanks again,
-Alex
Apr 14 '08 #11
ADezii
8,834 Recognized Expert Expert
Stewart & ADezii,

Thank you, as you point it out ADezii's post does contain a way to create the ranges I am looking for. I am just so new to any form of code. I am a network specialist who just got the DBA responsibilities handed to me. So I am totally out of my area of strength.

In looking at the answer with the "private code module" I just have no clue where to put that. I just got the Access for Dummies book so I can develop the basic knowledge to be able to implement these suggestions.

ADezeii I am sorry your advice just went over my head.

I will study some more and see how to work with what you all have given me.


Thanks again,
-Alex
Alex, nothing to be sorry for. Being handed the job of a DBA, even given the fact that you are a Network Specialist, is no small task. Let's take a different approach on this matter. How about providing me some realistic Test Data, say consisting of a couple hundred Records, if possible. I'll try to provide a couple of solutions for you in the form of Querys which you can then view and analyze. These Queries and the Test Database itself I will make available to you as an Attachment to a Post (a picture is worth a thousand words). If you feel as though this is a good idea, let me know, and I'll send you my E-Mail Address in a Private Message. You can then Attach the Data to an E-Mail Message sent to me.
Apr 14 '08 #12
Alex30093
8 New Member
Alex, nothing to be sorry for. Being handed the job of a DBA, even given the fact that you are a Network Specialist, is no small task. Let's take a different approach on this matter. How about providing me some realistic Test Data, say consisting of a couple hundred Records, if possible. I'll try to provide a couple of solutions for you in the form of Querys which you can then view and analyze. These Queries and the Test Database itself I will make available to you as an Attachment to a Post (a picture is worth a thousand words). If you feel as though this is a good idea, let me know, and I'll send you my E-Mail Address in a Private Message. You can then Attach the Data to an E-Mail Message sent to me.

Thanks that would be awesome.
Apr 14 '08 #13
Alex30093
8 New Member
Alex, nothing to be sorry for. Being handed the job of a DBA, even given the fact that you are a Network Specialist, is no small task. Let's take a different approach on this matter. How about providing me some realistic Test Data, say consisting of a couple hundred Records, if possible. I'll try to provide a couple of solutions for you in the form of Querys which you can then view and analyze. These Queries and the Test Database itself I will make available to you as an Attachment to a Post (a picture is worth a thousand words). If you feel as though this is a good idea, let me know, and I'll send you my E-Mail Address in a Private Message. You can then Attach the Data to an E-Mail Message sent to me.
I don't know if you tried to send me a private message or not. I am just checking in.
-Alex
Apr 15 '08 #14
ADezii
8,834 Recognized Expert Expert
I don't know if you tried to send me a private message or not. I am just checking in.
-Alex
Alex, I did send you my E-Mail Address in a Private Message but I'll send it again.
Apr 15 '08 #15
ADezii
8,834 Recognized Expert Expert

Thanks that would be awesome.
I've taken your basic Excel data, imported it into Access, randomly assigned names to the Employee IDs, then dumped it into a Table named tblAlex30093. From what you have previously indicated, you have no problem getting to this point. I then created an Intermediate Query which properly formats the data and assigned weekly intervals for the [Dep Date] Field. This Query is named qryTotalHours. The final step in this process was to create a Crosstab Query that summarizes Total Hours for each Employee, for specific Week Intervals. Total Sales spanning all Intervals is also calculated. I've manually restricted the Date Range from 3/1/2008 to 3/31/2008 and worked only on a subset of the data. You can make the Crosstab Query accept Date Parameters if you wish, and you can also include a greater Date Range. The Crosstab only works for the year 2008 right now. Enough already! Just download the Attachment Alex30093.zip, and let me know what you think.
Apr 17 '08 #16
Alex30093
8 New Member
I've taken your basic Excel data, imported it into Access, randomly assigned names to the Employee IDs, then dumped it into a Table named tblAlex30093. From what you have previously indicated, you have no problem getting to this point. I then created an Intermediate Query which properly formats the data and assigned weekly intervals for the [Dep Date] Field. This Query is named qryTotalHours. The final step in this process was to create a Crosstab Query that summarizes Total Hours for each Employee, for specific Week Intervals. Total Sales spanning all Intervals is also calculated. I've manually restricted the Date Range from 3/1/2008 to 3/31/2008 and worked only on a subset of the data. You can make the Crosstab Query accept Date Parameters if you wish, and you can also include a greater Date Range. The Crosstab only works for the year 2008 right now. Enough already! Just download the Attachment Alex30093.zip, and let me know what you think.
Thanks I will work on it and get back to you.
Apr 19 '08 #17

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

Similar topics

0
2509
by: Dalan | last post by:
Maybe this isn't as difficult as it seems, but cannot get it to work correctly. I have an Access 97 Form which contains 12 separate rows of text boxes that are used for entering monthly sales...
12
6334
by: Steve Elliott | last post by:
I have a query set up to gather together data between two specified dates. Shown in the query column as: Between #24/09/2004# And #01/10/2004# Is it possible to enter several different date...
2
2004
by: junkaccount | last post by:
Hello, Using Access 2000 I would like to create one report that returns sales data for various date ranges input by the user i.e. weekly, monthly, etc. This report is bound to a query that pulls...
67
7598
by: PC Datasheet | last post by:
Transaction data is given with date ranges: Beginning End 4/1/06 4/4/06 4/7/06 4/11/06 4/14/06 4/17/06 4/18/06 4/21/06 426/06 ...
1
2567
by: Matt | last post by:
Hi all, I have a database with a table storing a list of names, invoice dates, and invoice amounts. What I'm looking to do is to create a sum of the invouice amounts based on a range of...
2
1362
by: 663scott | last post by:
Hi, Two questions. Firstly how do I make a primary key case insensitive to allow username: John and john for example to be entered into it. My database is just a single table so would it matter...
22
4972
by: boliches | last post by:
I am trying to get a crosstab query (in access 2000) to group data by date range. TRANSFORM Sum(tblInvoice.InvBalance) AS SumOfInvBalance SELECT tblInvoice.DealerID, Sum(tblInvoice.InvBalance) AS...
1
2175
by: cryptotech2000 | last post by:
I am trying to filter this query based on date ranges but it don't seem to be working correctly, its probably something really simple if anyone can help me with this select...
8
4420
by: xzmilan | last post by:
Totally new to using loop through's but I have a feeling it's what I need. I have two tables tbl1 has a user ID and a change date, a date they updated their information tbl2 has the user ID...
0
7014
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
7180
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
7229
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...
1
6905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3108
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.