I have the table Vacation in mysql DB. The table has datetime_from and datetime_to. To have a vacation from Monday to Friday means there is only one record in the table with two timestamps
date_from = 'MONDAY_DATE 00:00:00'
date_to = 'FRIDAY_DATE 23:59:59'
The working time is from 8:00 to 16:00 every day. I would like to get all the working time that employee missed during his vacation (in hours for example). It's 8hrs a day x 5.
Im able to do that in 5 queries (one for every day and then sum up with PHP) as an intersection of date intervals BUT is it possible to perform it in only one mysql query?
SELECT SUM(date_to - date_from) as seconds_missed FROM Vacation
WHERE ...
That would give you the total number of seconds missed for an employee assuming the where clause matches it against a given user. I guess you could also add conditions in your where clause to only grab dates in a certain range (i.e. worked missed that week).
Related
So I have this time-log entry on my mysql database.
TABLE: timelogs
- id
- employee_id
- in (datetime)
- out(datetime)
I wanted to check the entries if there times in between 10PM to 6AM.
This is to calculate the night premium for each employee.
Say a sample entry,
- in: 2022-09-09 19:34:00
- out: 2022-09-10 01:29:00
This should result that this time-log has the range to calculate the night premium.
How do I check it using Eloquent?
You can simply use whereBetween clause and convert the columns with date times to time format using TIME function.
Here's an example assuming your model is called Timelogs.
Timelogs::whereBetween(DB::raw('TIME(in)'), array('22:00:00', '06:00:00'))
->orWhereBetween(DB::raw('TIME(out)'), array('22:00:00', '06:00:00'))
->get();
I have tons of records that are in my database table leadactivity basically I need to display all the records that were created in the first week of the current month, then also another query to display records in the second week, same for third week and then finally the fourth week of the current month.
I have a column called created_date which onupdate puts in the current timestamp
What is the best way to achieve this?
You can use date functions for this:
select la.*
from leadactivity la
where day(la.created_date) between 1 and 7 and
created_date >= curdate() + (1 - day(curdate())) day;
The above assumes MySQL syntax. Most databases have similar functionality, although the specific functions may differ.
I have a query that counts the "Xp" difference per day from my database, this all works as it should however it groups from midnight-midnight, what I would like to do is group 3am to 3am.
However another issue I think I may have is that my query may not always have the rows being the exact second at 3am due to the fact that it has to run a huge query and retrieve data from another website per user profile, so it should get all data after 3am, but before maybe 4am or something, so it has enough time to get all of the rows.
my current mysql is:
SELECT FROM_UNIXTIME(date, '%Y%m%d') AS YYYYMMDD, MAX(xp)-MIN(xp) AS xp_gain
FROM skills
WHERE userID = '$checkID'
AND skill = '$skill'
AND date >= '$date'
GROUP BY YYYYMMDD
ORDER BY date ASC
The best way to handle this is to add (if you can) another column that is just a DATE (not a DATETIME) and have this field rollover from one day to the next at 3am, (you can to this by subtracting 3 hours from the current time when doing the INSERT).
This gives you a couple of benefits, especially with a large number of rows:
It is much faster to query or group by a DATE than a range of
DATETIME
It will always query the rows at the exact second of 3am,
regardless of how long the query takes.
I am setting up a scheduled cron job that launches a PHP script every other Monday.
My goal is to run a SQL query that returns work orders based on my company's pay period - each pay period ends on the Friday before I run this scheduled cron job.
I have a SQL table which lists all the pay periods of my company, so this table has two column: payperiod_from (start date range) and payperiod_to (end date range). The work orders that I am pulling have a date assigned to them. I would like to be able to pull the work orders where their date is between payperiod_from and payperiod_to.
Since pay periods end every other friday and this cron job is scheduled on the Monday after that, I can simply use CURDATE() - 3 (3 days before Monday - Friday); to get the payperiod_to date, but how can I run a query that filters work orders who's date fall under the correct pay period?
Hopefully this makes sense to some of you guys.
You may run into trouble trying to set up a cron job for "every other week". There's a StackOverflow answer about that here; note the comment under the answer.
As for selecting work orders whose date falls under the correct pay period, if you're already calculating the pay period end as three days before CURDATE() why not forget about the pay_periods table (or whatever it's named) and calculate the pay period beginning as 16 days before CURDATE():
SELECT * FROM wkorder
WHERE wkorder.orderdate BETWEEN CURDATE() - INTERVAL 16 DAY AND CURDATE - INTERVAL 3 DAY
If you can't get the cron job to reliably run every other week and have to set it to weekly, then you can use the pay_periods table to filter out the off weeks. This will prevent any results if the prior Friday wasn't a pay-period end date because there won't be a match in the pay_period table:
SELECT * FROM wkorder
INNER JOIN pay_period ON
payperiod_to = CURDATE() - INTERVAL 3 DAY AND
wkorder.orderdate BETWEEN pay_period.payperiod_from AND pay_period.payperiod_to
Finally, please note that the queries above are syntactically correct but they're not fully tested. It's too much of a leap to go from the information in the question to actual table structures.
Current MySQL table schema: "Date" column with yyyy-mm-dd values
Assuming you are building a web app to rent out XBox's. Would you:
Create a a few checkboxes with "Every Monday," "Every Tuesday," "etc..." (Implication: If it's every Monday, how would you insert dates of only Monday's into the DB? Perhaps only insert the Monday's for the next three months initially and auto-increment and keep the tables light?"
Use a multi-datepicker for users to select multiple dates (Implication: User experience drops since the user will need to select more dates as time progresses?)
Other options?
How would add "hours" in addition to dates?
Store the data separately:
Start date - the start of the range
End date - the end of the range
Days of the week - the days of the week during the range (store this as a JSON array)
Hours per day - the hours per day (store this as a JSON object, with either start/end time per day of the week)
Try taking a look at Google Calendar add event page. They organize the times very smartly. It should give you some ideas for it.