Ms Access to show only previous week's records - ms-access

In MS Access I am trying to filter to show only records from previous week.
I have colums showing weeknumber based by record date.
Using DatePart("ww",Date(),2,2)-1 in criteria field works well if current week is not 1. It does not work to previous year.
How I can show only records from previous week or 2 weeks, even if it goes to previous year?

For the most recent week of data, start with something like this ...
YourDateField Between (Date() -7) AND Date()
That approach will not break when your target YourDateField range include dates from 2 years. Another advantage of that approach is the query can make use of an index on YourDateField, if you have one. In that case the db engine can examine the index to find which rows match your criterion and read only the matching rows instead of all rows in the record source. (If you don't have an index on YourDateField, add one to see if it speeds up the query significantly. You can drop the index if it's ineffective.)
Conversely using a WHERE condition based on the DatePart() function will force the db engine to examine each and every row in the query's record source. That can be a performance bottleneck which can be significant with a huge table, especially if it is a linked table which requires data be read across a network connection.
Edit: I misunderstood what you wanted. For the the week which started on the previous Monday, I used this condition:
Between Date() - ( 6 + Weekday(Date(), 2))
AND Date() - Weekday(Date(), 2)
The db engine will evaluate those expressions one time only when running the query, so while this approach is more complicated than what I first suggested, it can still offer the potential performance improvements over using DatePart() for every row in the data source.

HansUp gave me an idea to solve my problem.
Using Between (Date()-7-Weekday(Date(),2)+1) And (Date()-Weekday(Date(),2)+1), should give me previous calendar week. Weekday() returns daynumber of the week. Substracting weekday number (+1) from date should return Monday of the week.

Related

Database structure for storing schedules/cron job?

I am stuck with a problem. In an app's db, I am having a schedule table which will store user provided schedules. E.g
Daily
Every Week
Twice a Week
Every 3rd (or any user chosen) day of week
Every Month
Twice a month
Every x day of month
Every x month of year
And so on. These schedules will then provide reference point to schedule different tasks or identify their repeat-ance.
I am not able to think of a proper database structure for it. The best I can get is to have a table with following columns:
Day
Week
Month
Year
type
Then store the specified schedule in the related column and provide the type.
e.g Every week can go like 1 in week column and 1 (designated value for repeating whole) or something like that.
The problem with this approach is that this table is gonna be used very frequently and the data retrieved will not be straightforward. It will need calculation to know the schedule type and hence will require complex db queries to get each type of schedule.
I am implementing it in Laravel app if that can provide any other methodology. It's a SAAS app with huge amount of data related to the schedule table.
Any help will be very much appreciated. Thanks
I suggest you are approaching the problem backwards.
Devise several rules. Code the rules in your app, not in SQL. When inserting an event, pre-fill a calendar through the next 12 months with all occurrences of the event. Every month, go through all events and extend the "pre-fill" through another month (13 months hence).
Now the SELECTs are simple and fast.
SELECT ... WHERE date = '...'
has all the events for that day (assuming it is within 12 months).
The complexity is on inserting. But presumably you insert less often than you select.
The table with the event definitions would be only as complex as needed for your app to figure out what to do. Perhaps
start_date DATE,
frequency ENUM('day', 'week', 'month', ...)
multiplier TINYINT, -- this lets you say "every second week"
offset TINYINT, -- to get "15th of every month"
Twice a week would be two entries.
Better yet, there are several packages (in Perl, shell, etc) that provide a very rich language for expressing event-date-patterns. Furthermore, you may be able to simply 'call' it to do all the work for you!

Updating mysql table whilst shifting values

I have a table that I use for statistical purposes.
Its columns are id and 1,2,3,..,31 and pivot.
This table gives the number of views on each day for the last 31 days.
1 gives the number of views for yesterday.
14 gives the number of views for 14 days ago.
etc ...
(pivot is just used to calculate the number of views)
I would use a cron job every day to update this table, but how would I go about "shifting" all the values to the side ( value column 15 would become value column 16; new value for column 1; delete value for column 31)
Define a table with only two columns — "date" and "views"
INSERT a new row in the table with the view count for that day when the CRON job runs
Modify your application query to read through this new table over a custom date range, which could be 31 days or anything else either — please have a look at this link to get an idea:
MySQL Query - Records between Today and Last 30 Days
Not really sure how pivot is being used here. However, I'm almost certain that if you're using it to store the sum of the views, it could as well be computed by using SUM() or GROUP BY without having to need a separate column in the table
As far as data archival / removal is concerned, your daily CRON job could be modified to include a DELETE query (as the last step) which cleans up records older than a certain date. Again, you could use the link above to get your "target" date
.
I apologise that this might sound like a little too long a solution to what you've asked for. However, I feel, this approach should help you organise and maintain the table in question in a better way.

Database structure for variable column names [duplicate]

I am writing a script which will counts the number of ticket as per as different conditions and store the count in a summary table. I am unable to understand how to structure my table since there will be more than 1 variables.
The script will run every week and with each execution a new week will be added. Once the new month starts and scripts execute, a new month will be added and once new year starts a new year will be added. So in a nutshell I have columns which are also variables. Not sure how to handle it and structure my db.
Have columns Date Service Count.
Like so.
Date Service Count
16 May 2016 Service1 35
Then when you go to display them in the report pivot them as you want. Probably better to make date an INT and either an auto number PK or composite PK of Date and Service.
Wrong approach. Do not splay an array across columns. Instead have rows for the data. This lets you trivially "add" another month.
For displaying, well that is an application problem, no a MySQL problem. Sure, you can write specialized queries to "pivot" the data from rows to columns, but it is messy.

sql query count daily difference / count new rows, no date column

I have a database for our local real estate listings, there are no dates or timestamp columns.
I would like to be able to get out just the rows that were added in the past day or two.
Can anyone point me in the right direction to get this data out?
EDIT:
Each new row does get a new id number which is incrementally higher, so I can ORDER the results by newest.
Would it be possible to save my query count in a file, or in another database, each day, then calculate the difference and use that as my number of new listings?
"Would it be possible to save my query count in a file, or in another database, each day, then calculate the difference and use that as my number of new listings?"
I understand that you can't change the table structure to add a date...so instead, I suggest to have a cron job at midnight that will create a record with a date and the higher ID at this moment. This way, you will be able to finde a range of ID for a specific date...
If you relayed on query count, you will get problem when you will start to delete some rows...

Access 2010 Query for current month

I have built an Access database to keep track of Quality Assurance Monitors for our team. Our team has several leads that oversee the reps on the team.
I want to build a query the will return all of the QA's for a specified lead for the current month. I have criteria for specifying a lead and have got that to work, but every time I try to set the criteria for the current month, it ends up returning no results.
Searching Google has repeatedly suggested using the Month(Now()), but that doesn't work either.
How can I write this query?
Answer inspired by #Justin-rentmeester but refined to always return the desired results:
Add the following to the WHERE clause of your query:
MyDateField BETWEEN
DateSerial(Year(Date()),Month(Date()),1)
AND
DateAdd("s", -1, DateAdd("m", 1, DateSerial(Year(Date()),Month(Date()),1))), DateSerial(Year(Date()),Month(Date()),1)
I reccommend that you use SQL view to add this criterium.
This approach also works with dates that include times, and with months with less than 31 days.
Put this code on criteria field- Between Date()-Day(Now()-1) And Date()
I edit my answer!
Assuming workdate is your date field.
A shorter where clause can be
where Year(workDate) = Year(Date()) and Month(workDate) = Month(Date())
This will filter all records for the current month.
If you want to further filter for records up to today add
and workdate <= Date()
This may not be necessary if you are not storing future dated records in your table.
Good luck.
To do this in the Design view in Access, select all of the columns you want in the report and on the date column you want to restrict to the current month, for the Criteria put: Between
DateSerial(Year(Date()),Month(Date()),1)
And
DateSerial(Year(Date()),Month(Date()),31)