473,545 Members | 2,788 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 7234
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 'fConvertWeekRa nge' 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 fConvertWeekRan ge 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 'fConvertWeekRa nge' 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 'fConvertWeekRa nge' 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 'fConvertWeekRa nge' 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

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

Similar topics

0
2517
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 information with dates and amounts. What I'm trying to accomplish is having a Query to scan each of the rows and be able to show data from one row or...
12
6350
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 ranges, ie between 24/09/2004 and 01/10/2004 together with 05/10/2004 and 07/10/2004 ? If I enter the "Between" criteria on different lines it...
2
2008
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 the info from a table containing all data. How would I set up my report to prompt for the multiple data ranges I want to see? Thanks, Jason
67
7617
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 4/30/06 I am looking for suggestions on how to find the date ranges where there were no transactions.
1
2572
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 invoice dates. e.g. Sum of invouice amounts for invoice date range between date1 and date2 then sum of invoice amount for date range between 3 and 4.
2
1368
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 if I define username as a primary key? I dont think it would. Secondly, I would like to work out the difference between two date ranges. If i send...
22
4985
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 FROM tblInvoice GROUP BY tblInvoice.DealerID PIVOT Format(Now(),"ww"); I know the above code is wrong! My question is how do I get the query...
1
2179
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 ReceivedDate,orderstatus WHERE RecieveDate BETWEEN ''20071101' AND '20071231' ,count(case when dcoc = '88' then 1 end) as cnt88 ,sum(case when dcoc = '88'...
8
4421
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 and an amount they asked to be reimbursed on a specifice date. tbl1 change date is in one column. I want to get the sum of reiembursement from...
0
7499
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7689
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7456
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7786
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6022
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5359
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3490
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.