Find current work shift from table - mysql

I'm creating a table for shift times, each day has it's own start/end time. The table below only shows Sunday & Monday, but the actual table will have 7 days. I want to be able to display the current shift (desc) based on current time. This is straight forward until I get to 3rd shift, specifically past midnight. If it's 5am Monday, that is actually 3rd shift Sunday. How can I go about this, either to make a table work, or if there might be a better way to go about it?
ID Desc Sunday_Start Sunday_End Monday_Start Monday_End
1 1st 07:00:00 15:00:00 07:00:00 15:00:00
2 2nd 15:00:00 23:00:00 15:00:00 23:00:00
3 3rd 23:00:00 07:00:00 23:00:00 07:00:00
I noticed this post,
DateTime range issue when time passes midnight, but it looks like it is primarily for a repeatable shift from day to day.
The customer really wants each day to be different. I haven't decided if I will do in MySQL or just XML. So, one question, is there a better way to do this? I was searching for a calendar and using a repeating appointment, but I didn't see anything free that was worthwhile. Anyone have any insight on this?
I'm using Visual Studio 2015 vb.net & MySQL.

Your shift table should be like this...
Unlike excel, databases should always have ONE record (Row) per "Item"...
Note I used Shift_Length instead of Shift_End. Shift_Length is in hours. Shift_End can therefore be calculated from the two.
Now searching for the current shift simply involves determining if the current day & time is within Shift_Length of the indicated day and Start_Time.

Related

Laravel array filtering

Developers, I am working on a report that I have to display the companies ranking based on some conditions for the date range which comes from front. Ex: last week, I have done that, but now I want to show how many weeks that a particular company be in same rank. If I am checking the past week I have to check the ranking for each week from the year start. If last week first position company and other weeks first position company is same I have make the count as increasing accordingly. When I querying the data for each week using the for loop it is taking around 42s to process and display the data. Also I tried to fetch whole data from first week of the year to current week then I filtered the array but this also takes long time. Can anyone give any other ideas to overcome this? Thanks in advance.
As far as I understand your problem - then storage of aggregated data should help you.
Create a table in the database, let's say "archive_rating", with 3 fields: week_number (let from January 1, 2000), company_id, company_position in your rating. Don't forget the index for the week_number field.
At 00:00:00 every Monday, run a background task that will save the positions for each company to an archive table.
This will allow you not to calculate a rating for each week from the beginning of the year. You will already have it.

How do I get a table that shows events per user over a time interval?

This is very hard to explain in just one question, however here goes.
In MySQL database I have a table called video that has all the videos created.
The structure is something like this
id
owner
title
created_at
I have another table user, something like this
id
name
created_at
The goal is to get the following graph
x axis is the month #
y axis is the # of videos per month
As you can see, it shows how many videos each user created each month. BUT the first month is always 30 days from the date the user is created. So for user Ben it is going to be from 2016-10-01 to 2016-11-01 but for Rick it is from 2016-08-01 to 2016-09-01
So for Ben's first month it is different from the first month of Rick by approximately 60 days.
But in the graph I want to show all of them as month #1
My solution for now is to "normalize" the data. So to get the data for Ben I took the difference between a fixed date (I set it to 2016-01-01) and calculated the difference of days.
Then I subtract the number of days for all the videos and this way all the videos first month is going to be 2016-01-01 to 2016-02-01
This works, but I'm really not sure if this is the best approach.
Anyone with a better idea of how to do this?
Thanks
I was told about a better way to do it.
Instead of normalizing the dates as I was doing, we just create a new property
I updated my create_video event to have a property like age_of_user. Then I can group_by age_of_user to see how many videos they uploaded in whatever number month.
Something like
{
age_of_user: {
days: 64,
weeks: 9
months: 2,
}
}
Each event is created with this new property.
And when I query I group by month and by user and get all the data I need
This way I don't need to mess up with the timestamp of the video and I can use it in different queries.

Mysql query group by week with specific start and end

I searched stack overflow and have come so close yet have not found if this is possible.
I want to group a weekly query from 11:30 am Friday to 11:29 am Friday for each week.
adding days and hours only partially work. I need to keep this in a mysql query not coding in php, etc.
You need to give more information on your table structure and some sample date. If I have understood correctly then below is simple pseudo code which you can start use to start framing your requirement .
select something from table where date between 1:30 am Friday and 11:29 am
group by trunc(date).
Note :
- Format the date in where condition using to_Date.
- Week definition might be different in database so you need to manipulate.

Payroll System: Database structure for flexible storage of weekend off scheme

Objective:
The Leaves Management and Payroll system requires the official weekend hodiday scheme stored in the database to be referred to while generating the monthly salary for each employee in the organisation. Generating flexible Database Schema for storing the weekend scheme is the objective.
Problem:
The database schema should be flexible enough to allow the changes in the definition of a weekend. The weekend in our scenario may be defined as:
1. Sunday
2. Sunday + 1st or 2nd or 3rd or 4th Saturday
3. Sunday + (1st and 2nd) or (1st and 3rd) or (1st and 4th) or (2nd and 3rd) or (2nd and 4th) or(3rd and 4th) Saturday
4. Sunday + all Saturdays
maybe store the scheme like that
11111 - means Sunday and all Sats
10000 - means only Sunday
11010 - means Sunday and 1st and 3rd Sats
so first number is Sunday, second - 1st Saturday, etc
You could the use Bitwise OR to check for some values like
checking for 1st saturday
SELECT * FROM table_name WHERE holiday_scheme | 01000 > 0
Hope that helps.
Bitwise logic can be clever, but I believe that scheme does not scale well as this expression does probable translate in a table scan. If that 'table_name' is big, it can punish you a lot on joins.
It can be better if you create an work_day_schedule and associate that schedule to the 'table_name' (ex.: employee). Now, a table scan on the work_day_schedule would not hurt much and you can do what bitwise magic you want.

Sorting by month in MS Access

I am using an MS Access db to track some tasks during the year. Each task has a due Month. I do not want to use exact dates as the convention in my team is to refer to the month. I don't want to store the dates in a date format as team members will be entering the due month by hand.
Is it possible to sort my fields in date order if the date is stored as a text string month? (eg. January, February rather than 31/01/2009, 28/02/2009).
If so, what would such a query look like?
Thanks in advance.
If you are storing only the month name, your will first need to convert to a date to get a month number, use a lookup table (MonthNo, MonthName) or use the Switch function. Here is an example of converting to a date:
SELECT Month(CDate(Year(Date()) & "/" & [MonthNameField] & "/1")) AS MonthNo
FROM Table
However, there is probably a good argument for storing a date based on the month name entered, this would prevent any confusion about years.
This should work
SELECT *
FROM TableName
OrderBy Month(date_field)
I would store the month as an integer 1-12 then you can easily sort them.
I would make a date field.
I would store 1/1/2009 for January 2009, 2/1/2009 for February 2009, and so forth. For display purposes, I'd format it so that it displayed only the month (or Month + Year -- can't imagine how you wouldn't want the year).
This makes it possible to take advantage of date operations on the field without messy conversions of text to date formats.
Thank you all for your responses. Sorry for the delay in responding - I'm working on this issue again now.
For clarity, the DB is to be used to track a schedule of events within a 12 month period. The year does not need to be stored as everything in the DB is referring to the same year. A new copy of the DB will be made at the beginning of 2010.
I'm really keen to actually store the month as a word rather than any kind of value or date field as when bulk adding tasks I will likely edit the table directly rather than use a form.
I realise this is dead but google brought me here while i was looking so thought I would add to it:
I had this problem myself (Access 2010) and found a decent answer here: http://www.vbforums.com/showthread.php?503841-How-to-Convert-MonthName-to-Value(Microsoft-access-2003)
So what I did was have a Query which pulled out the DISTINCT months from my table. Then in the design view i added another column with MonthNo: Month(CDate("1 " & [Month]))and sorted the query on this column
hope this helps someone if not the OP.