I would like to know if it's possible to fetch X no of business days (date datatype) via a single DB call in mysql.
The list of holidays are stored in a table. So, the days (starting from CURDATE) which do not have entries in that table are considered to be working days.
Thanks!
Does the holiday table also include weekends?
Create a numbers table with a single column (num, say) and rows 1 through some-large-value - this'll come in handy. LEFT JOIN the holidays table to this table on "holidayday" = (CURDATE + INTERVAL num - 1 DAY), add a WHERE to exclude the holidays and then order this query by num ascending and LIMIT the query to the X rows.
Related
I am in the process of creating a maintenance query for a MySQL table where I need to mark records as deletable (Update a column value from 0 to 1) based on a date field in the table.
Pretty simple but the problem I have is that the criteria to mark the record as deletable varies depending on the account ID (acc_id column).
For example, the table will have records from multiple accounts (acc_id field) and there is a separate accounts table that has a column (retention_age) that I need to use to determine whether a record can be marked as deletable.
Example:
SELECT *
FROM files
WHERE DATE(file_downloaded_date) < DATE_SUB(CURDATE(), INTERVAL 30 DAY)
AND deleted = 0;
So for example, in the table with the records to update, there may be 100 records for acc_id = 1, 250 records for acc_id = 2 etc.... acc_id 1 is set to mark records as deletable after 14 days, acc_id 2 is set to mark as deletable after 30 days.
I'm trying to figure out if I can create an SQL query to do this all in one query or if I'll need to create a separate query for each account.
I'm thinking that if there would be a way to replace the '30' in INTERNAL 30 DAY, with the retention_age value for the acc_id of each record that would be the best way but I don't know how to do that or if that will even work.
Any suggestion would be greatly appreciated.
You should be able to achieve this using a LEFT JOIN to get the respective account and the use its retention_age as the INTERVAL:
DELETE files FROM files
LEFT JOIN accounts ON accounts.id = files.acc_id
WHERE
DATE(file_downloaded_date) < DATE_SUB(CURDATE(), INTERVAL accounts.retention_age DAY)
AND deleted = 0
I have around 200,000 records and each record has DATETIME field. I have been trying to select records by every n hours using the DATETIME field. For example if n = 1; 1 record is selected for every 1 hour. I haven't been able to find many examples online.
Table: Product
Fields: id, name, description, lastSoldOn
Well, you can convert the date/time to seconds and use arithmetic to select one value from each n-hour period:
select min(datetimecol)
from t
group by floor(to_seconds(datetimecol) / (3600 * $n));
If you need the complete record, you can use join or exists to match back to the original table.
I have 3 columns of importance in my table, each of which store a date.
ID
Inpatient_date
ER_date
I am trying to find which people (ID) went to the ER (ER_date) within 30 days of seeing the hospital (Inpatient_date). I need to be able to look at every date within the inpatient_date column, and compare to every date in the ER_date column. Then from those results, further narrow it down by having the row with the ER_date that was within 30 days, and the row housing the Inpatient_date have the same persons ID.
I am at a loss on how to do this.
You can do this using exists:
select t.*
from t
where exists (select 1
from t t2
where t2.er_date > t.inpatient_date and
t2.er_date < t.inpatient_date + interval 30 day
);
I am interpreting your question as "visits the ER 1-30 days after being in the hospital". If you are looking for 30 days before, or 30 days before and after, you can adjust the condition in the subquery.
I have attendance data for employees stored in the table attendance with the following column names:
emp_id (employee ID)
date
type (leave, absent, etc.)
(there are others but I'm omitting them for the sake of simplicity)
My objective is to retrieve all dates of the given month on which the employee was on leave (type = 'Leave') and the last leave taken in the last month, if any.
It's easy to do it using two queries (I'm using PHP to get process the data), but is there any way this can be done in a single query?
I'm answering my own question so as to close it. As #bpgergo pointed out in the comments, UNION will do the trick here.
SELECT * FROM table_name
WHERE type="Leave" AND
date <= (CURRENT_DATE() - 30)
Select the fields, etc you want then se a combined where clause using mysql's CURRENT_DATE() function. I subtracted 30 for 30 days in a month.
If date is a date column, this will return everyone who left 1 month or longer ago.
Edit:
If you want a specific date, change the 2nd month like this:
date <= (date_number - 30)
I have a MySQL db that stores orders, and has a date field that gets populated when the order reaches a certain point.
I want to create a cron job that checks for all orders where this date is in multiples of 'weeks' ago. For example:
Date stored: 12/1/2012
this row would be returned if the cron job triggered on the following days:
12/8/2012
12/15/2012
12/22/2012
12/29/2012
etc...
How do i structure the MySQL query to fetch data in this way?
You can use modular arithmetic:
SELECT * FROM my_table WHERE DATEDIFF(CURDATE(), my_date) % 7 = 0