Get records from NOT EXISTS and IN subquery - mysql

I have a query that needs to perform the following: Get all attendee accounts that do not have meetings scheduled during the following dates and times.
You can run samples here. I already have a date and two times added in the URL.
In my above link results you'll see Alex Hendry listed. He has a meeting at 11:00. Since I am getting only times at 11:30 and 12:00 his name appears. However, if I change the times to 11:00 and 11:30 his name does not appear; I understand that since 11:00 falls into the conditions he shouldn't appear.
How can I change my above query to return Alex Hendry for the 11:30 availability?

Related

Find next available time slot in "calendar"

I have the following MySQL tables:
ServiceProviders- id, ...other irrelevant columns
ProvidersWorkHours - id, providerId, day(enum[1,2,3,4,5,6,7]), startTime(time), endTime(time)
Service- id, duration, ...other irrelevant columns
Groups- id, serviceId, providerId,size, duration
GroupReservations- groupId, customerId.
ServiceProviders have their own workHours (when they're available to work), they can create Groups of different sizes and of different durations (they can be longer or shorter than regular service duration). I need to find next available time slot for regular service duration that is not reserved or a group still not completely filled (for example group of size 2 that has only 1 reservation is a valid one).
Expected outcomes:
Current date: 2022-06-20 18:24 (MONDAY)
Regular service duration: 45 minutes
Provider workHours: [MONDAY 08:00-17:00, TUESDAY 08:00-17:00, WEDNESDAY 08:00-17:00]
Some expected scenarios:
We have new provider, with no groups, no reservations. Expected nextAvailableSlot 2022-06-21 08:00
We have only one full group at 2022-06-27 12:00. Next availableSlot still should be 2022-06-21 08:00
We have group of size 2 that has only 1 reservation at 2022-06-21 08:00. Next available slot should be 2022-06-21 08:00
We have full groups till 2022-06-21 16:00 (last one ends at 16:00). Next availableSlot should be 2022-06-21 16:00.
As I was thinking we can't go through solution that I found in other places (join same reservations table with itself, in my case it would be groups). It's bad since I can have no groups, or my first group a week ahead of currentDate. Probably what would work is to take currentTime and keep adding regular serviceTime and check all the constrains, until it does not intersect with any reservations, or until it find a group that isn't filled yet. Not sure how to do it in MySQL, though. Any ideas or other solutions?

How to add calendar event to database that repeats every x day/week?

For a user management software I try to implement a feature, where employees can provide their availabilities at a specific location.
I got a form:
Location [> LOC1; LOC2, LOC3]
Start [YYYY-MM-DD] [HH:ii]
End [YYYY-MM-DD] [HH:ii]
Full day [X]
Frequence [> ONCE; EVERY MONDAY; EVERY 2ND WEEK]
This goes to the follow database table scheme:
eid | lid | start | end | allay | frequence
Where frequence is NULL for once, 1 for every day of the week provided by start value, and 2 for every 2. week.
With that I have the following logic:
Where Y is always greater than X (both include date and time).
As far as I can think of, this is covering all possible cases. Logically I can use that to query a presence, but I am struggling with the dates, that are available in infinity. My logic is, if start and end date are the same, and frequence is not null, then this row will be available always and in infinity.
E.g. employee is available from 2022-03-31 10:00:00 to 2022-03-31 18:00:00, allday 0, frequence 1, then he is available every Thursday between 10am and 6pm.
So far, so good, so logic, but when I now want to use that, I am struggling, and I want to ask you experts, if this is the right way to implement that.
Issues I see:
Query: Is an employee available at a specific date / time? For the infinity cases, I don't know hot to query that.
If there is a frequent rule in place, and I add another rule that overwrites the same rule, I don't know how to avoid duplicates.
E.g.:
2022-03-31 10:00:00 2022-03-31 18:00:00 0 1
2022-04-14 17:00:00 2022-04-17 20:00:00 0 null
In consequence at 2022-04-14 the employee is available from 10am to 8pm.
And last issue I see:
If I want to build a calendar that shows a specific month (e.g. October 22) and I want all availabilities in that month, how can I get every single date and time value for every single day in that month for rows like:
2022-03-31 10:00:00 2022-03-31 18:00:00 0 1
2022-03-31 00:00:00 2022-12-31 00:00:00 1 2
As my database design is bad maybe, I don't see a value in adding a mysql fiddle with some sample data. But anyhow if you wish to see that, let me know, and I'll update my question.
What I'd prefer maybe, is, to add a row for every day where an employee is available. This would be easier and more intuitive and I can handle to logic once, when I add an availability, but how would I handle infinite dates there? I could assume that the app will not be used longer than 20y and add every date in the next 20 years?!

SSRS formatting several records in on line

I have an employee database table that has the times that every employee went into work or went for break. The table has a record for every time a employee signed in or out like this:
Name   | Type        |  Date
john sil  |  ClockIN    |  10/11/2020 9:00 AM
john sil  | BreakIN     |  10/11/2020 12:00 PM
john sil  |  BreakOut  | 10/11/2020 12:30 PM
john sil  | ClockOut   |  10/11/2020 5:00 PM
I would like to group those records in one to appear all in one line instead of four. Something like this:
Name    |  Date  |   ClockIn  |  BreakIn  |   BreakOut  |  ClockOut
John sil  |  10/11/2020  |  9:00 AM  |  12:00 PM  |  12:30PM  |  5:00 PM
Any help will be appreciated. Thanks in advance.
A Matrix in SSRS was made for this kind of data.
Add a Matrix to your report.
In the Grouping Window, go to Group Properties. Add a Grouping to the Row and Group On Date without the time. Sort by the date expression.
=Format(Fields!Date.Value, "yyyy-MM-dd")"
This will combine all the records for a single day.
Use the Date field for the Cell value and use 'mm/DD/yyyy' as the Format Property of the Date text box.
Right Click on the RowGroup and Add Group -> Parent Group.
Group on Employee and Sort by Employee. Use the Name for the Text Box value. This will break up the days by employee.
Click on the Column Group in the Group Window and go to Group Properties. Group On Type.
Go to the Sort tab. Add a Sort By
=IIF(Fields!Type.Value = "ClockIN", 1, 2)
This will make the Clock In come up first.
And then add a second level by Fields!Type.Value. The sorting by name will work for the other Types. The Column grouping will break up the daily data by the different punches.

Finding available timeslots in a day based on existing records in MySQL

I have a table that has these fields:
How would I find all available time slots for that day (based on the day starting at 7am and ending at 10pm) that aren't currently in this table? For example, on one particular day, if all timeslots from 7am till 10pm were taken bar one at 6pm till 7pm, that would be the one result.
The duration of each time slot does not vary - they all last one hour.
I have tried many different things, but I have a feeling I am so far off, it is hardly worth posting what I've tried.
Any help would be greatly appreciated!
One thing I would think about would be having a "hours table" with all the values you wanna check. Then, with a left join and selecting only null values, you'd get only the values you haven't assigned. I built a SQLfiddle http://sqlfiddle.com/#!2/66b441/6 to check it with some dummy values to show how this works:
SELECT h.slot
FROM hours h
LEFT JOIN deliveries d
ON ( h.slot = d.start_time AND date_stamp = '2014-04-04' )
WHERE start_time IS NULL
Check the data in the SQLfiddle, if you know the slots and they don't overlap, with that you will get for the date the values
SLOT
January, 01 1970 07:00:00+0000
January, 01 1970 10:00:00+0000

Use of if condition in MYSQL queries

Does an IF condition in the where clause of a MySQL query slow down the execution drastically?
Here is the one sample query:-
select * from alert_details_v adv
where (if(day(last_day(now()))<DAY(adv.alert_date),
day(last_day(now())),DAY(adv.alert_date))-adv.alert_trigger_days)<=day(now());
Sample data:
alert_id alert_date alert_trigger_days
==================================================
1 2013-09-14 00:00:00 6
2 2013-09-13 00:00:00 5
alert_date: Some user input date
alert_trigger_days: Number of days before the actual date the alert be triggered.
Brief about query logic:-
Here I am trying to find if the last day of the current month is less than the day of the alert_date (database column). Whichever day comes before would be considered.
Basically this table is meant for storing alert information. So if the user has chosen 30th of some month and the alert is recurring monthly then for February it would not find the day 30th and hence would not show the record.
My question is: does a query with if conditions (as in the sample query above) in where clause slows down the execution of the query drastically or slightly, if there are hundreds of thousands of records in the table?
This may entirely depend upon your table and data. Sometimes it may help in increasing the performance and sometimes it may degrade your performance.