I'd like to ask you, if there is any way in mysql to do this. I know it can be done by cycle, but feeling like there has to be a better way.
I have table vacation
id | date_from | date_to
1 2017-02-16 2017-02-19
2 2017-02-18 2017-02-21
3 2017-02-12 2017-02-19
4 2017-02-19 2017-02-21
The thing is, that user picks the date range he wants to reserve, like 2017-02-14 to 2017-02-24 and I have to check whether he can still request it, because only 4 people can request vacation simultaneously.
So I need query which will check whether in user selected range (for example 2017-02-14 to 2017-02-24) are for any day 4 or more rows.
Thanks in advance for your answers.
You can do this for any day:
select count(*)
from vacation v
where '2017-02-14' between date_from and date_to;
You can extend this to a range, by specifying each day:
select dte, count(v.id)
from (select date('2017-02-14') as dte union all
select date('2017-02-15') as dte union all
select date('2017-02-16') as dte union all
select date('2017-02-17') as dte union all
select date('2017-02-18') as dte union all
. . .
) d left join
vacation v
on d.dte between date_from and date_to
group by d.dte
having count(*) > 4;
It might be simpler to loop through on the application side. Or, to use a calendar table if you have one.
Select all rows in that date range and count the results:
SELECT count(*) FROM vacation WHERE :input_from BETWEEN date_from AND date_to OR :input_to BETWEEN date_from AND date_to
Related
I have a table which has the following data:
+-----------+-----------+
| Date_from | Date_to |
+-----------+-----------+
| 20-NOV-19 | 22-NOV-19 |
+-----------+-----------+
| 10-NOV-19 | 21-NOV-19 |
+-----------+-----------+
| 14-NOV-19 | 26-NOV-19 |
+-----------+-----------+
I need a query to find the sum of the difference between date_from and date_to.
Example:
The difference between 20-Nov-19 and 22-Nov-19 is 2 days
The difference between 10-Nov-19 and 21-Nov-19 is 11 days but the query has to consider it as 9 days because days 20-Nov, 21-Nov are already considered in the first row.
The difference between 14-Nov-19 and 26-Nov-19 is 12 days but the query has to consider it as 4 days because days 14-Nov to 22-Nov are already considered in the above rows.
The query result should be
15 days (2+9+4)
Any help would be much appreciated.
You can use below query to get total.
Please change Table_name with your actual table name
SELECT SUM(TIMESTAMPDIFF(DAY,Date_from,Date_to)) as total FROM Table_name
I used window functions in a sub-query to calculate the difference between date_from and date_to and then subtracting any overlapping days
SELECT SUM(days) FROM
(SELECT CASE WHEN LEAD(date_from) OVER w < date_to THEN DATEDIFF(date_to, date_from) + DATEDIFF(LEAD(date_from) OVER w, date_to)
ELSE DATEDIFF(date_to, date_from)
END AS days
FROM test
WINDOW w AS (ORDER BY date_to)) as d
Note though that this produces the result 16 days, not 15 as in the question, but then again so does
SELECT DATEDIFF('2019-11-26', '2019-11-10')
You can use CTE to generate all the dates between the range and get the distinct count().It will give you the total number of days as 17. Since you want difference you have to subtract by 2(start date and end date)
WITH recursive Date_Ranges AS (
select datefrom as Dt,dateto from mydates
union all
select dt + interval 1 day , dateto
from Date_Ranges
where dt < dateto)
select count(distinct(dt))-2 from Date_Ranges
DEMO HERE
Hi Programming Master,
I need help with this. This is an employee data, the NULL in TerminationDate refer to current date, it's mean that the employee are still working.
I need to find longest time (in days) where there is no one hired or terminated.
Table Name : Employee
Column Name : ID, HireDate, TerminationDate
Employee
ID HireDate TerminationDate
1 2009-06-20 2016-01-01
2 2010-02-12 NULL
3 2012-03-14 NULL
4 2013-09-10 2014-01-01
5 2013-09-10 NULL
6 2015-04-10 2015-05-01
7 2010-04-11 2016-01-01
8 2012-05-12 NULL
9 2011-04-13 2015-02-13
I have developed process of what need to do
Combine data in HireDate and TerminationDate (it should have 18 rows)
Order the date
Find the difference between each date from Row(n) and Row(n-1)
Get the max difference
However I don't know how to do it in MySQL or if it is even possible. I wonder if there is any other method? Please help me
This is rather complicated in MySQL, prior to version 8. But you can do:
select dte, next_dte,
datediff(coalesce(next_dte, curdate()), dte) as diff
from (select dte,
(select min(d2.dte)
from ((select hiredate as dte
from t
) union -- intentionally remove duplicates
(select terminationdate as dte
from t
where teminationdate is not null
)
) d2
where d2.dte > d.dte
) next_dte
from ((select hiredate as dte
from t
) union -- intentionally remove duplicates
(select terminationdate as dte
from t
where teminationdate is not null
)
) d
) d
order by diff desc
limit 1;
Note that this finds the the most recent period, based on the current date. You can adjust this by replacing curdate() with whatever cutoff date you have in mind. If you don't want the most recent period, add where next_dte is not null to the outer query.
Let's say I have a table that says how many items of something are valid between two dates.
Additionally, there may be multiple such periods.
For example, given a table:
itemtype | count | start | end
A | 10 | 2014-01-01 | 2014-01-10
A | 10 | 2014-01-05 | 2014-01-08
This means that there are 10 items of type A valid 2014-01-01 - 2014-01-10 and additionally, there are 10 valid 2014-01-05 - 2014-01-08.
So for example, the sum of valid items at 2014-01-06 are 20.
How can I query the table to get the sum per day? I would like a result such as
2014-01-01 10
2014-01-02 10
2014-01-03 10
2014-01-04 10
2014-01-05 20
2014-01-06 20
2014-01-07 20
2014-01-08 20
2014-01-09 10
2014-01-10 10
Can this be done with SQL? Either Oracle or MySQL would be fine
The basic syntax you are looking for is as follows:
For my example below I've defined a new table called DateTimePeriods which has a column for StartDate and EndDate both of which are DATE columns.
SELECT
SUM(NumericColumnName)
, DateTimePeriods.StartDate
, DateTimePeriods.EndDate
FROM
TableName
INNER JOIN DateTimePeriods ON TableName.dateColumnName BETWEEN DateTimePeriods.StartDate and DateTimePeriods.EndDate
GROUP BY
DateTimePeriods.StartDate
, DateTimePeriods.EndDate
Obviously the above code won't work on your database but should give you a reasonable place to start. You should look into GROUP BY and Aggregate Functions. I'm also not certain of how universal BETWEEN is for each database type, but you could do it using other comparisons such as <= and >=.
There are several ways to go about this. First, you need a list of dense dates to query. Using a row generator statement can provide that:
select date '2014-01-01' + level -1 d
from dual
connect by level <= 15;
Then for each date, select the sum of inventory:
with
sample_data as
(select 'A' itemtype, 10 item_count, date '2014-01-01' start_date, date '2014-01-10' end_date from dual union all
select 'A', 10, date '2014-01-05', date '2014-01-08' from dual),
periods as (select date '2014-01-01' + level -1 d from dual connect by level <= 15)
select
periods.d,
(select sum(item_count) from sample_data where periods.d between start_date and end_date) available
from periods
where periods.d = date '2014-01-06';
You would need to dynamically set the number of date rows to generate.
If you only needed a single row, then a query like this would work:
with
sample_data as
(select 'A' itemtype, 10 item_count, date '2014-01-01' start_date, date '2014-01-10' end_date from dual union all
select 'A', 10, date '2014-01-05', date '2014-01-08' from dual)
select sum(item_count)
from sample_data
where date '2014-01-06' between start_date and end_date;
I have a table like this:
id | created_on
1 2013-09-03 20:05:09
2 2013-09-05 17:03:13
...
How do I write a query to return a result of record counts that was created from Date X to Date Y in 7-day intervals?
So the result would look like this:
count | created_on
4 2013-09-17 00:00:00
2 2013-09-24 00:00:00
1 2013-09-31 00:00:00
10 2013-10-07 00:00:00
...
You can go to the beginning of the week by subtracting the day of the week. Here is one way to do that:
select date(created_on - interval dayofweek(created_on) day), count(*)
from t
group by date(created_on - interval dayofweek(created_on) day);
If this is not the day you want the week to start, then you can add an offset day.
Group by the date field, floored to the week:
SELECT
count(*),
YEARWEEK(created_on) as week
FROM
yourtable
GROUP BY week
This assumes that created_on is a type that can be interpreted as a date:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_yearweek
This will get you weekly groupings, but you may want to then convert that field (which will look like YYYYWW) back to something more readable.
You can try this
SELECT created_on, count( id ) AS count
FROM `test_table`
WHERE created_on
BETWEEN '2013-09-01'
AND '2013-10-10'
GROUP BY WEEK( created_on )
I'm trying to make a query to get rush hours for everyday on a specific month.
The table I have looks like this:
id idproduct created_at
1 021354684 2011-10-01 20:25:48
2 033546835 2011-10-01 20:30:15
3 055965654 2011-10-01 20:45:20
4 012975343 2011-10-02 14:03:36
5 021354684 2011-10-02 15:55:48
6 033546835 2011-10-02 16:30:15
7 055965654 2011-10-02 16:45:20
8 012975343 2011-10-02 18:53:36
9 021354684 2011-10-03 08:55:48
10 033546835 2011-10-03 09:30:15
11 055965654 2011-10-03 14:03:20
12 012975343 2011-10-03 14:03:36
What I try to get is something like this...:
day rush_hour number_of_rows
1 20:00 3
2 16:00 5
3 14:00 4
Is it possible to get a table like this? can you guys help me?
I made a mistake, sorry for this. The number of rows should be the total of items sold that day, not in that hour :( sorry.
http://sqlfiddle.com/#!2/5b87b/7
First, count every day's every hour's count (into a view, because we will use it twice below):
CREATE VIEW hours AS
SELECT
DATE( created_at ) AS d,
HOUR( created_at ) AS h,
COUNT(*) AS c
FROM item
GROUP BY DATE(created_at), HOUR(created_at);
Final query:
SELECT
hours.d AS `day`,
hours.h AS `rush_hour`,
hours.c AS `count`
-- get the max count for every day
FROM (
SELECT
d, -- the day
MAX(c) as c -- the count
FROM hours
GROUP BY d
) AS maxc
-- find the actual hour(s) with the max count for every day:
INNER JOIN hours ON hours.c = maxc.c
AND hours.d = maxc.d;
You're going to want to look at the MySQL Date Functions, they offer you some help with this
SELECT
day(created_at) as day,
hour(created_at) as rush_hour,
count(1) as num_rows
FROM item
GROUP BY
day(created_at), hour(created_at)
http://sqlfiddle.com/#!2/62a15/2/0
Try this:
SELECT dayofyear(created_at) as day, hour(created_at) as rush_hour, count(*) as number_of_rows
FROM table
GROUP BY dayofyear(created_at), hour(created_at);
Here it is without making a view:
SELECT ddd.day, eee.rush_hour, ddd.maxo
FROM
(select day, max(num_rows) as maxo from (
SELECT
day(created_at) as day,
hour(created_at) as rush_hour,
count(1) as num_rows
FROM item
GROUP BY
day(created_at), hour(created_at)
) as groupo group by day) as ddd
LEFT JOIN
(SELECT
day(created_at) as day,
hour(created_at) as rush_hour,
count(1) as num_rows
FROM item
GROUP BY
day(created_at), hour(created_at)
) as eee on ddd.day=eee.day and ddd.maxo=eee.num_rows
I could imagine it being formatted more nicely or having more relevant aliases, but there's just so much subselecting going on here.
And thanks SQLfiddlers for putting the data there.
And I think that if you have two hours tied for the highest number of whatever it is you are counting, they both will show up, so you'll get two (or more) records returned for that day of the month.