Query to Get 7 Days Prior Data (1 Viewer)

JithuAccess

Member
Local time
Today, 08:28
Joined
Mar 3, 2020
Messages
297
Hello Guys,

What is the Query Syntax to find a Week's prior Data?

In my Table I have a field "Max Date" and there is Value From 01-Jan-2021 to 31-Jan-2021. I want to create a Query to get the data only from 25-Jan-2021 to 31-Jan-2021.

I have created a query and in the Criteria of MaxDate I put like Date()-7 but this will get the Data between 02-Mar-2021 to 09-Mar-2021. Then I put like this in the Criteria "MaxDate-7" and it says Data Type Mismatch in the Criteria Expression.

Could you please let me know how to solve this?

Thanks
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:28
Joined
Oct 29, 2018
Messages
21,456
Hi. You could maybe use DMax() to find the latest date from the table and then subtract 7 days from it.
 

JithuAccess

Member
Local time
Today, 08:28
Joined
Mar 3, 2020
Messages
297
Hi. You could maybe use DMax() to find the latest date from the table and then subtract 7 days from it.

Thanks a lot for your reply. This is actually storing the Total Hours spend working on a Particular File. There are Fields Like Start Date 1, Start Time 1, End Date 1 and End Date 1 Up to 30 Days. With a Union Query I found the Latest Date worked on a file. Now I want to calculate Total Hours spend a week on a file. For Example if I spend 4 Hours each on 25-Jan-2021 to 30-Jan-2021 and 8 Hours on 31-Jan-2021 I want to find the Total Hours spend on that week as 24:00

Thank You
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:28
Joined
Oct 29, 2018
Messages
21,456
Thanks a lot for your reply. This is actually storing the Total Hours spend working on a Particular File. There are Fields Like Start Date 1, Start Time 1, End Date 1 and End Date 1 Up to 30 Days. With a Union Query I found the Latest Date worked on a file. Now I want to calculate Total Hours spend a week on a file. For Example if I spend 4 Hours each on 25-Jan-2021 to 30-Jan-2021 and 8 Hours on 31-Jan-2021 I want to find the Total Hours spend on that week as 24:00

Thank You
Have you tried using a Totals query with the Sum() function?
 

JithuAccess

Member
Local time
Today, 08:28
Joined
Mar 3, 2020
Messages
297
Have you tried using a Totals query with the Sum() function?

Thank You for your reply. I guess if I use the Sum() function it will returns me the Total Hours spend on the File. But I want to find the Sum of Records of MaxDate Minus 7

For 30 Days the Total sum let us assume as 120 Hours and for Last 7 Days Before the Max Date (31-Jan-2021 - 7 which is Between 25-Jan-2021 and 31-Jan-2021) is 20, I want to find just those 20 Hours, Not 120 Hours.

Apologies if this is confusing.

Thanks
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:28
Joined
Oct 29, 2018
Messages
21,456
Thank You for your reply. I guess if I use the Sum() function it will returns me the Total Hours spend on the File. But I want to find the Sum of Records of MaxDate Minus 7

For 30 Days the Total sum let us assume as 120 Hours and for Last 7 Days Before the Max Date (31-Jan-2021 - 7 which is Between 25-Jan-2021 and 31-Jan-2021) is 20, I want to find just those 20 Hours, Not 120 Hours.

Apologies if this is confusing.

Thanks
Hi. Not sure you're picking up on what I'm trying to tell you. If you have a list of records for the whole month and you use Sum() on them, you should get the total hours for the whole month. But, if you use DMax() to get the latest date and then use it with DMax()-7 to filter your list of data, then you should only have enough records for the last week of the month data. And when you use the Sum() function on those records, you should get less hours than what you got before without the filter.
 

JithuAccess

Member
Local time
Today, 08:28
Joined
Mar 3, 2020
Messages
297
Hi. Not sure you're picking up on what I'm trying to tell you. If you have a list of records for the whole month and you use Sum() on them, you should get the total hours for the whole month. But, if you use DMax() to get the latest date and then use it with DMax()-7 to filter your list of data, then you should only have enough records for the last week of the month data. And when you use the Sum() function on those records, you should get less hours than what you got before without the filter.

Thanks a lot.

This is my Query

1615315488073.png


This is my Query Result

1615315511385.png


Now I am getting the sum of all 28 Days. To find the Total of Last 7 Days, what change I have to make in my query.

Thanks
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:28
Joined
Oct 29, 2018
Messages
21,456
I was thinking something more along these lines.
SQL:
SELECT Sum([TotalHours]) AS Total FROM [QueryName] WHERE [LatestDate] Between DMax("LatestDate","QueryName})-7 And DMax("LatestDate","QueryName")
 

JithuAccess

Member
Local time
Today, 08:28
Joined
Mar 3, 2020
Messages
297
I was thinking something more along these lines.
SQL:
SELECT Sum([TotalHours]) AS Total FROM [QueryName] WHERE [LatestDate] Between DMax("LatestDate","QueryName})-7 And DMax("LatestDate","QueryName")

Many Thanks for your Help. This is my Query

SELECT Sum([TotalHours]) AS Total FROM [Copy Of Copy Of Query15] WHERE [LatestDate] Between DMax("[LatestDate]","[Copy Of Copy Of Query15]"-7) And Dmax("[LatestDate"],"[Copy Of Copy Of Query15]");

But I am getting an Error like:

1615316575772.png



Thanks
 

Attachments

  • 1615316432149.png
    1615316432149.png
    7.4 KB · Views: 225

theDBguy

I’m here to help
Staff member
Local time
Today, 07:28
Joined
Oct 29, 2018
Messages
21,456
Sorry, not sure what happened. Looks like the code I posted replaced a double quote with a curly bracket. Let's try it again.
SQL:
SELECT Sum([TotalHours]) AS Total FROM [QueryName] WHERE [LatestDate] Between DMax("LatestDate","QueryName")-7 And DMax("LatestDate","QueryName")
 

JithuAccess

Member
Local time
Today, 08:28
Joined
Mar 3, 2020
Messages
297
Sorry, not sure what happened. Looks like the code I posted replaced a double quote with a curly bracket. Let's try it again.
SQL:
SELECT Sum([TotalHours]) AS Total FROM [QueryName] WHERE [LatestDate] Between DMax("LatestDate","QueryName")-7 And DMax("LatestDate","QueryName")

It Works Perfect!!!


Thanks a lot For your Help Sir
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 10:28
Joined
Feb 19, 2002
Messages
43,224
Although theDBGuy seems to have solved the problem, you might want a more generic solution that will give you data for ANY 7 day period.

I would add an unbound textbox to the form. Set its format to date so that Access knows you will be entering a date and act appropriately. You can set the default value for this unbound control to dmax("LatestDate", "QueryName") Then your query would be:

BetweenForms!yourForm!yourcontrol -7 And Forms!yourForm!yourControl

That allows you to use the default of the latest 7 days or by changing the contents of the control to some other date, select data for the seven days ending at that date.
 

Users who are viewing this thread

Top Bottom