I have the following problem. I have a mysql table that has a startdate and and enddate. Each row is considered active between those dates. Some rows are no longer active, but have been active in the past. For example the following table:
id start end
1 2014-11-11 00:00:00 2015-01-31 23:59:59
2 2014-09-25 10:16:14 2015-06-01 23:59:59
3 2013-12-24 00:00:00 2014-12-01 23:59:59
4 2014-08-13 00:00:00 2016-01-31 23:59:59
5 2013-09-11 00:00:00 2014-09-10 23:59:59
My actual table has way more data than that. Now I need to know what the peak amount of concurrent active rows is without knowing when that peak actually occured. How would I do this in SQL? In the example 4 rows are active at the same time (1-4, not 5) in the time between 2014-11-11 and 2015-01-31 23:59:59. The actual peak time doesn't matter to me as much as the peak amount itself.
Thanks for your help
Find different timestamps of interrest using UNION ALL, count number of active tasks at these timestamps:
select ts, (select count(*) from tablename t2
where timestamps.ts between t2.start and t2.end) as count
from (select start as ts
from tablename
union all
select end
from tablename) as timestamps
order by count desc
limit 1
Finally order descending and pick the highest value only!
(From a non MySQL user, so some details may be wrong... Please comment if that's the case and I'll edit!)
Related
This is the current table I have.
ID Start_Date End_Date
6446 2018-01-01 00:00:00 2018-04-01 00:00:00
6848 2018-05-01 00:00:00 2018-05-31 00:00:00
3269 2016-11-09 00:00:00 2016-11-21 00:00:00
7900 2018-11-07 00:00:00 2018-11-30 00:00:00
4006 2017-04-06 00:00:00 2017-04-30 00:00:00
Is there a way to get the number of active events per week? Some events might run past a few weeks. Event ID is distinct and can be used to count.
Please help and happy to furnish more info if required.
EDIT 1: The dataset I want is
2019 week 1 - 60 active events
2019 week 2 - 109 active events
I know about WEEK(datetime), however that does not capture the event being active for subsequent weeks.
The issue is that I don't capture the number of active events after the week they are started.
EDIT 2: Week would be defined as the integer returned using the week() function in mysql on a date object. My data is only for 2019.
Try to use count() function in MySQL.
SELECT COUNT(*) FROM your_table WHERE Start > 'start-date' AND End < 'end-date'
Give a try to below query
SELECT
IFNULL(DATE_FORMAT(Start_Date, '%Y WEEK %U'), 0) AS STARTDate,
IFNULL(DATE_FORMAT(End_Date, '%Y WEEK %U'), 0) AS ENDDate,
IFNULL(COUNT(ID),0) AS total,
group_concat(ID)
FROM `event`
where Start_Date < End_Date
Group by STARTDate;
I found an answer, but encountered a new problem.
Building upon Kranti's code, the answer is as follows.
SELECT
EXTRACT(WEEK FROM starting_date) AS STARTDate,
EXTRACT(WEEK FROM ending_date) AS ENDDate,
discount_type,
COUNT(ID) AS total
FROM `event`
where starting_date < ending_date
Group by 1,2
What this gives me is the number of events that have happened from Week 1 - 3, Week 1-4 etc, so on and so forth.
Afterwards, we do a left join with the weeks of interest, on the condition where the week numbers are in between STARTDate and ENDDate. Due to how a left join works, it will duplicate rows for all the rows that fulfill the specific condition.
We follow up with a groupby and sum, which will give us the number of events that were active, for each week.
What I am trying to achieve is grab data from the database, depending on the time.
For example, I may have multiple prices for an item, and I would like the new price to be effective based on the time and date. So I can schedule price changes in advance.
id link_id datetime price
-------------------------------------------
2 11 2016-11-03 00:00:00 1020
3 11 2016-11-03 01:00:00 1050
4 11 2016-11-03 03:00:00 1090
Let's say the time is 2016-11-03 00:59:00, when a user queries the db they will se the price-1020. But when they query the db a minute later at 2016-11-03 01:00:00 they should get price-1050.
Have tried this WHERE datetime < UTC_TIMESTAMP(), however this does not solve my problem. Also it only needs to select one entry, this selects multiple.
Is there a way MySQLi can do this?
If you are only looking for one item, I would expect something like this:
select p.*
from prices p
where p.item_id = $item_id and -- however you are representing items
p.datetime <= now() -- or UTC Timestamp if that is how the date/time is represented
order by p.datetime desc
limit 1;
I could speculate that "link_id" refers to "items".
How can I select all rows from a table where a date column is within a specific range of dates, at a given period (e.g. every 14 days)?
The table has a date column with most every date represented, possibly multiple times. The range is defined by a start date and an end date. The period is a number of days. For example:
Start: 2016-01-01 (friday)
End: 2016-12-31 (saturday)
period: 14 (days)
For the above, the query should return rows for every other Friday in 2016. That is, it should return the rows for the following dates:
2016-01-01
2016-01-15
2016-01-29
2016-02-12
2016-02-26
2016-03-11
2016-03-25
2016-04-08
2016-04-22
2016-05-06
2016-05-20
2016-06-03
2016-06-17
2016-07-01
2016-07-15
2016-07-29
2016-08-12
2016-08-26
2016-09-09
2016-09-23
2016-10-07
2016-10-21
2016-11-04
2016-11-18
2016-12-02
2016-12-16
2016-12-30
Currently, this is done in a stored procedure where a loop fills a temp table with the target dates, which is later joined on. However, I am trying to rewrite this code to step away from stored procedures.
What would be the best way to get the desired rows without using the stored procedure & a temp table? Keep in mind that (one of) the table(s) is quite large at around 1M records indexed on date, so any calculated values might impact the performance severely.
Alternatively, I could calculate all dates in the interval in PHP/RoR and use a massive IN clause, but hopefully there is a better solution.
Try this:
table_name1 is your table
date1 the date field
"2022-01-02" the start (twice included)
"2022-01-10" the end
3 the interval
SELECT date1
FROM table_name1
WHERE date1 BETWEEN "2022-01-02" AND "2022-01-10"
AND (DATE("2022-01-02") - date1) % 3 = 0;
Tested it with MySQL 5.6.
I have a MySQL table similar to this:
item | order | start date | end date
------------------------------------------
1 1 2015-09-15 2015-09-20
2 1 2015-09-15 2015-09-20
1 2 2015-09-20 2015-09-25
2 2 2015-09-20 2015-09-25
What I want to do is execute a query that will check if any end-dates are within 7 days of a future start date, and return the result. Does anyone know how this could be done?
EDIT: Should be more specific I suppose - the start date and end date of an order (say in this case order 2 from the example table) can be within 7 days of each other. I want to check if order 1's end date is within 7 days of order 2's start date. Sorry if that wasn't clear before.
You can use datediff function.
select * from table_name
where
start_date > curdate()
and datediff(end_date,start_date) between 0 and 7
MySQL Table: -
ID | From_DateTime | To_DateTime
1 2014-09-01 10:00:00 2014-09-10 22:00:00
Explanation:
I have added 2 columns in database table for Date and Time range. The data shown above means that from 01-09-2014 to 10-09-2014 is the date range and 10:00:00 to 22:00:00 is the time range.
Positive Scenario - Now I am passing 2014-09-05 15:00:00 in my query which comes into Date and Time both range. What I need is the ID from the query.
Negative Scenario - Now I am passing 2014-09-05 23:00:00 in my query which comes into Date comes into range but TIME is not in the specified range so I should get 0 result.
I have no idea about database queries and that is why I am posting it to here to get some help from the database experts.
Something like this should work. May need some tweaking.
SELECT ID FROM (
SELECT ID FROM <tablename>
WHERE DATE('<dateTimeValue>') BETWEEN DATE(From_DateTime) AND DATE(To_DateTime)
AND TIME('<dateTimeValue>') BETWEEN TIME(From_DateTime) AND TIME(To_DateTime)
UNION
SELECT 0 AS ID FROM DUAL
) AS a LIMIT 1
SELECT id FROM date_time
WHERE From_DateTime < '2014-09-05 15:00:00'
AND To_DateTime > '2014-09-05 15:00:00';
here date_time is table name...
Don't forgot to put date in single quotes