Bringing in Schedules for Access calculations - ms-access

I really don't know how to ask this question or title it but here I go. I work in a school system and I have created a database for some psychologists to use to track their referrals. By state rules,they have 60 days from the date of their first meeting to finish the process. Weekends still count but HOLIDAYS DO NOT. I do not really know how to use the calender we have so that we have an accurate Calculation. For instance, with Holidays accounted for, if a kid was started today, he would need to have everything finished on 1/18/2013 That is 60 days from now based on our schedule. Does anyone have any idea where I should start?
Edit
Ok, so I now have a Calender table. Here is my issue. I have my column that I used to indicate which days are used in calculating my 60 days. Weekends can be used in that calculation. HOWEVER, they cannot be used in the result. If the 60th day lies on a Sunday or Saturday, then the date would need to go to the Friday before. I guess my first issue is really, how do I limit my calculation to the dates in my calender table?

This can be easy with a calendar table.
PARAMETERS start_date DateTime;
SELECT TOP 1 sub.the_date
FROM
(
SELECT TOP 60 the_date
FROM tblCalendar
WHERE
the_date>=[start_date]
AND work_day=True
ORDER BY the_date
) AS sub
ORDER BY sub.the_date DESC;
That query is based on the assumption you have set work_day to True for the dates you want evaluated. IOW, work_day will be False only for your organization's holidays.
For sample code to create and load your calendar table, see the CreateTable_calendar() and LoadCalendar() procedures at Using Start Date and End date in Access query. To initially assign all dates including weekend days as work days, make this change in LoadCalendar().
'rs!work_day = Not (Weekday(dte) = vbSunday Or _
' Weekday(dte) = vbSaturday)
rs!work_day = True
Finally, manually edit the table to change work_day to False for your holidays.

You can check the weekday to ensure you have not chosen a weekend:
SELECT TOP 1 CalDate, WDay
FROM (SELECT Top 60 c.CalDate,Weekday([Caldate]) AS WDay
FROM Calendar c
WHERE c.Holiday=False) a
WHERE WDay Not In (1,7)
ORDER BY CalDate DESC

Related

What's the difference between the two SQL statements?

This is a question from leetcode, using the second query I got the question wrong but could not identify why
SELECT
user_id,
max(time_stamp) as "last_stamp"
from
logins
where
year(time_stamp) = '2020'
group by
user_id
and
select
user_id,
max(time_stamp) as "last_stamp"
from
logins
where
time_stamp between '2020-01-01' and '2020-12-31'
group by
user_id
The first query uses a function on every row to extract the year (an integer) and compares that to a string. (It would be preferable to use an integer instead.) Whilst this may be sub-optimal, this query would accurately locate all rows that fall into the year 2020.
The second query could fail to locate all rows that fall into 2020. Here it is important to remember that days have a 24 hour duration, and that each day starts at midnight and concludes at midnight 24 hours later. That is; a day does have a start point (midnight) and an end-point (midnight+24 hours).
However a single date used in SQL code cannot be both the start-point and the end-point of the same day, so every date in SQL represents only the start-point. Also note here, that between does NOT magically change the second given date into "the end of that day" - it simply cannot (and does not) do that.
So, when you use time_stamp between '2020-01-01' and '2020-12-31' you need to think of it as meaning "from the start of 2020-01-01 up to and including the start of 2020-12-31". Hence, this excludes the 24 hours duration of 2020-12-31.
The safest way to deal with this is to NOT use between at all, instead write just a few characters more code which will be accurate regardless of the time precision used by any date/datetime/timestamp column:
where
time_stamp >= '2020-01-01' and time_stamp <'2021-01-01'
with the second date being "the start-point of the next day"
See answer to SQL "between" not inclusive

fetch rows from table on basis of custom week order

finding records within a week, i tried this
SELECT * FROM tblbulletin WHERE YEARWEEK(publisheddate) = YEARWEEK(CURRENT_DATE)
but it returned the records as like calendar week. I.E. the records were between sunday and saturday of a current week.
how can i write a code to set different start of week? and how to display records within custom start of week to custom end of week?
You can include a value for the mode argument which will set what the first day of the week is. If no mode is included the default is to select Sunday as the first day.
See http://www.techonthenet.com/mysql/functions/yearweek.php for a list of the different modes available.
as #gerard says, use YEARWEEK with the optional {mode} value set to 1 - for week starting monday e.g.
SELECT * FROM tblbulletin WHERE YEARWEEK(publisheddate,1) = YEARWEEK(CURRENT_DATE,1)
NOTE: WEEK has a table with the other possible values for {mode}

SQL Execute on Certain Date Query

I have a small database with my website and I would like to execute a subtraction from everyone's account on a certain date like:
UPDATE Loan SET balance = (balance-amount)
"WHERE Day = 15"
Obviously the last part is just a guess but I have no due date in the loan table so sadly I can't do subtract when curDate= duedate sort of thing so I thought I would just do a set day when everyone's balance is subtracted on a set day.
You can get the DAY() from NOW() function and can use it in your query like
UPDATE Loan SET balance = (balance-amount)
WHERE DAY(NOW()) =15
See fiddle to get day from NOW()
You have to maintain some kind of reference date in order for this to work.
If everyone pays on the same day of the month, add a table that preserves the last time you extracted the fee. You'll calculate the next fee date using the period and the last time you extracted it.
Remind me not to go to your website. Too many fees.

SQL Statement Database

I have a Mysql Table that holds dates that are booked (for certain holiday properties).
Example...
Table "listing_availability"
Rows...
availability_date (this shows the date format 2013-04-20 etc)
availability_bookable (This can be yes/no. "Yes" = the booking changeover day and it is "available". "No" means the property is booked for those dates)
All the other dates in the year (apart from the ones with "No") are available to be booked. These dates are not in the database, only the booked dates.
My question is...
I have to make a SQL Statement that first calls the Get Date Function (not sure if this is correct terminology)
Then removes the dates from "availability_date" WHERE "availability_bookable" = "No"
This will give me the dates that are available for bookings, for the year, for a property.
Can anyone help?
Regards M
Seems like you've almost written the query.
SELECT availability_date FROM listing_availability
WHERE availability_bookable <> 'NO'
AND availability_date >= CURDATE()
AND YEAR(CURDATE()) = YEAR(availability_date)
I think I understand, and you'll obviously confirm. Your "availability_booking" has some records in it, but not every single day of the year, only those that may have had something, and not all are committed, some could have yes, some no.
So, you want to simulate All dates within a given date range... Say April 1 - July 1 as someone is looking to book a party within that time period. Instead of pre-filling your production table, you can't say that April 27th is open and available... since no such record exists.
To SIMULATE a calendar of days for a date range, you can do it using MySQL variables and join to "any" table in your database provided it has enough records to SIMULATE the date range you want...
select
#myDate := DATE_ADD( #myDate, INTERVAL 1 DAY ) as DatesForAvailabilityCheck
from
( select #myDate := '2013-03-31' ) as SQLVars,
AnyTableThatHasEnoughRows
limit
120;
This will just give you a list of dates starting with April 1, 2013 (the original #myDate is 1 day before the start date since the field selection adds 1 day to it to get to April 1, then continues... for a limit of 120 days (or whatever you are looking for range based -- 30days, 60, 90, 22, whatever). The "AnyTableThatHasEnoughRows" could actually be your "availability_booking" table, but we are just using it as a table with rows, no join or where condition, just enough to get ... 120 records.
Now, we can use this to join to whatever table you want and apply your condition. You just created a full calendar of days to compare against. Your final query may be different, but this should get it most of the way for you.
select
JustDates.DatesForAvailabilityCheck,
from
( select
#myDate := DATE_ADD( #myDate, INTERVAL 1 DAY ) as DatesForAvailabilityCheck
from
( select #myDate := '2013-03-31' ) as SQLVars,
listing_availability
limit
120 ) JustDates
LEFT JOIN availability_bookable
on JustDates.DatesForAvailabilityCheck = availability_bookable.availability_date
where
availability_bookable.availability_date IS NULL
OR availability_bookable.availability_bookable = "Yes"
So the above uses the sample calendar and looks to the availability. If no such matching date exists (via the IS NULL), then you want it meaning there is no conflict. However, if there IS a record in the table, you only want those where YES, you CAN book it, the entry on file might not be committed and CAN be in your result query of available dates.

Trouble of a MySql query not returning the right information

This is the query I have already:
use willkara;
select EngagementNumber,AgentID, EntertainerID, StartDate, EndDate, ContractPrice, ContractPrice/DateDiff(EndDate,StartDate) AS PricePerDay
FROM EA_Engagements
where StartDate <= '1999-8-13'
and EndDate >= '1999-8-8'
ORDER BY EngagementNumber;
And this is the problem:
I need a list of engagements that occurred between 8/8/1999 and 8/13/1999. I only want to see the engagements that started on or after 8/8/1999 and ended on or before 8/13/1999. For each of those engagements, I need to know how long (in days) the engagement was, and the IDs of the entertainer and the agent, and the contract price per day of entertainment. Remember, when we compute the length of an engagement, we include both the day it started and the day it ended. Please sort the information in Engagement number order. [2 rows]
8 columns needed; Last column must be labeled PricePerDay
For some reason, some of the end dates are 8-15 and 8-19 and it's only suppose to be the dates that end on the 13th.
Since they are two fields, it would be theoretically possible for someone to put in a start date that's older than the end date and vice versa leading to incorrect results. I'd adjust your query accordingly, do a between or something similar.