I have a table with three columns: planning_start_date - planning_end_date - quantity.
For example I have this data:
planning_start_date | planning_end_date | quantity
2019-03-01 | 2019-03-31 | 1500
I need to split the value 1500 into multiple rows with the adverage per day, so 1500 / 31 days = 48,38 per day.
The expected result should be:
date daily_qty
2019-03-01 | 48,38
2019-03-02 | 48,38
2019-03-03 | 48,38
...
2019-03-31 | 48,38
Anyone with some suggestions?
Should you decide to upgrade to MySQL 8.0, here's a recursive CTE that will generate a list of all the days between planning_start_date and planning_end_date along with the required daily quantity:
WITH RECURSIVE cte AS (
SELECT planning_start_date AS date,
planning_end_date,
quantity / (DATEDIFF(planning_end_date, planning_start_date) + 1) AS daily_qty
FROM test
UNION ALL
SELECT date + INTERVAL 1 DAY, planning_end_date, daily_qty
FROM cte
WHERE date < planning_end_date
)
SELECT `date`, daily_qty
FROM cte
ORDER BY `date`
Demo on dbfiddle
In MySLQ 8+, you can use a recursive CTE like this:
with recursive cte(dte, planning_end_date, quantity, days) as (
select planning_start_date as dte, planning_end_date, quantity, datediff(planning_end_date, planning_start_date) + 1 as days
from t
union all
select dte + interval 1 day as dte, planning_end_date, quantity, days
from cte
where dte < planning_end_date
)
select dte, quantity / days
from cte;
Here is a db<>fiddle.
In earlier versions, you want a numbers table of some sort. For instance, if your table has enough rows, you can just use it:
select (planning_start_date + interval n.n day),
quantity / (datediff(planning_end_date, planning_start_date) + 1)
from t join
(select (#rn := #rn + 1) as n
from t cross join
(select #rn := 0) params
) n
on planning_start_date + interval n.n day <= planning_end_date;
You can use any table that is large enough for n.
Related
I have a single line in MySQL table: volunteers
user_id | start_date | end_date
11122 | 2017-04-20 | 2018-02-17
How can I find how many times the 3rd day or 24th day of a month appears? (i.e. 2017-05-03, 2017-06-03, 2017-12-24, 2018-01-24) I'm trying to get to the following count:
Sample Output:
user_id | number_of_third_day | number_of_twenty_fourth_day
11122 | 10 | 10
I look at the documentation online to see if there is a way I can say (pseudo):
SELECT
day, COUNT(*)
FROM volunteers
WHERE day(between(start_date, end_date)) in (3,24)
I tried to create a calendar table to no avail, but I would try to get the days, GROUP BY day, and COUNT(*) times that day appears in the range
WITH calendar AS (
SELECT start_date AS date
FROM volunteers
UNION ALL
SELECT DATE_ADD(start_date, INTERVAL 1 DAY) as date
FROM volunteers
WHERE DATE_ADD(start_date, INTERVAL 1 DAY) <= end_date
)
SELECT date FROM calendar;
Thanks for any help!
This one is more optimized since I generate date range by months not days as other questions, so its faster
WITH RECURSIVE cte AS
(
SELECT user_id, DATE_FORMAT(start_date, '%Y-%m-03') as third_day,
DATE_FORMAT(start_date, '%Y-%m-24') as twenty_fourth_day,
start_date, end_date
FROM table1
UNION ALL
SELECT user_id,
DATE_FORMAT(third_day + INTERVAL 1 MONTH, '%Y-%m-03') as third_day,
DATE_FORMAT(twenty_fourth_day + INTERVAL 1 MONTH, '%Y-%m-24') as twenty_fourth_day,
start_date, end_date
FROM cte
WHERE third_day + INTERVAL 1 MONTH <= end_date
)
SELECT user_id,
SUM(CASE WHEN third_day BETWEEN start_date AND end_date THEN 1 ELSE 0 END) AS number_of_third_day,
SUM(CASE WHEN twenty_fourth_day BETWEEN start_date AND end_date THEN 1 ELSE 0 END) AS number_of_twenty_fourth_day
FROM cte
GROUP BY user_id;
Demo here
A dynamic approach is.
but creating the dateranges, takes a lot of time, so you should have a date table to get the dates
CREATE TABLE table1
(`user_id` int, `start_date` varchar(10), `end_date` varchar(10))
;
INSERT INTO table1
(`user_id`, `start_date`, `end_date`)
VALUES
(11122, '2017-04-20', '2018-02-17')
,(11123, '2019-04-20', '2020-02-17')
;
Records: 2 Duplicates: 0 Warnings: 0
WITH RECURSIVE cte AS (
SELECT
user_id,
`start_date` as date_run ,
`end_date`
FROM table1
UNION ALL
SELECT
user_id,
DATE_ADD(cte.date_run, INTERVAL 1 DAY),
end_date
FROM cte
WHERE DATE_ADD(date_run, INTERVAL 1 DAY) <= end_date
)SELECT user_id,
SUM(DAYOFMONTH(date_run) = 3) as day_3th,
SUM(DAYOFMONTH(date_run) = 24) as day_24th
FROM cte
GROUP BY user_id
user_id
day_3th
day_24th
11122
10
10
11123
10
10
fiddle
In last MySQL version you can use recursion:
-- get list of all dates in interval
with recursive dates(d) as (
select '2017-04-20'
union all
select date_add(d, interval 1 day) from dates where d < '2018-02-17'
) select
-- calculate
sum(day(d) = 10) days_10,
sum(day(d) = 24) days_24
from dates
-- filter 10 & 24 days
where day(d) = 10 or day(d) = 24;
https://sqlize.online/sql/mysql80/c00eb7de69d011a85502fa538d64d22c/
As long as you are looking for days that occur in every month (so not the 29th or beyond), this is just straightforward math. The number of whole calendar months between two dates (exclusive) is:
timestampdiff(month,start_date,end_date) - (day(start_date) <= day(end_date))
Then add one if the start month includes the target day and one if the end month includes it:
timestampdiff(month,start_date,end_date) - (day(start_date) <= day(end_date))
+ (day(start_date) <= 3) + (day(end_date) >= 3)
I am trying to split a specific date range into chunks of 3 days to find the number of records per each three day chunks.
For example, assume I have this table:
User
Minimum Date
Maximum Date
Consecutive Days
1
09/20/2021
09/29/2021
10
And I want to produce this table:
User
Minimum Date
Maximum Date
1
09/20/2021
09/22/2021
1
09/23/2021
09/25/2021
1
09/26/2021
09/28/2021
The reason I ended it off there is because the remaining days are not enough to make up 3 days.
You need in something like
WITH RECURSIVE
cte AS (
SELECT User,
MinimumDate,
MinimumDate + INTERVAL 2 DAY MaximumDate, -- N-1
MaximumDate FinalDate
FROM sourcetable
WHERE MinimumDate + INTERVAL 2 DAY <= MaximumDate -- N-1
UNION ALL
SELECT User,
MinimumDate + INTERVAL 3 DAY, -- N
MinimumDate + INTERVAL 5 DAY, -- 2*N-1
FinalDate
FROM cte
WHERE MinimumDate + INTERVAL 5 DAY <= FinalDate -- 2*N-1
)
SELECT User,
MinimumDate,
MaximumDate
FROM cte
ORDER BY 1, 2;
or (not tested)
WITH RECURSIVE
cte1 AS ( SELECT (MAX(DATEDIFF(MaximumDate, MinimumDate)) DIV 3) - 1 maxrange
FROM sourcetable ),
cte2 AS ( SELECT 0 num
UNION ALL
SELECT num + 1 FROM cte2 WHERE num < maxrange )
SELECT User,
MinimumDate + INTERVAL 3 * num DAY MinimumDate,
MinimumDate + INTERVAL 2 + 3 * num DAY MaximumDate
FROM sourcetable
JOIN cte2 ON MinimumDate + INTERVAL 3 * num DAY <= MaximumDate
ORDER by 1,2
I'm trying to get all my result of last 30 days, getting 0 when the count is 0.
My query is:
SELECT substring(mc.publication_date, 1, 10) AS title, count(mc.id) AS quantity
FROM mymyv_cards mc
WHERE mc.publicated = 1 AND STR_TO_DATE(substring(mc.publication_date, 1, 10), "%d-%m-%Y") BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE()
GROUP BY substring(mc.publication_date, 1, 10)
And I get this:
But I would like the result was like:
06-04-2021 --> 1
07-04-2021 --> 2
08-04-2021 --> 0
09-04-2021 --> 0
10-04-2021 --> 0
............... etc etc
I don't know how to do that, can you help me?
What you need is a list of all the dates. If you don't have one handy, you can create on using a recursive CTE:
with recursive dates as (
select curdate() as dte, 1 as lev
union all
select dte - interval 1 day, lev + 1
from dates
where lev < 30
)
select *
from dates;
Then you incorporate this into your query with a left join:
with recursive dates as (
select curdate() as dte, 1 as lev
union all
select dte - interval 1 day, lev + 1
from dates
where lev < 30
)
select d.dte, count(mc.id)
from dates d left join
mymyv_cards mc
on mc.publicated = 1 and
str_to_date(left(mc.publication_date, 10), '%d-%m-%Y') >= d.dte and
str_to_date(left(mc.publication_date, 10), '%d-%m-%Y') < d.dte + interval 1 day
group by d.dte;
Note that a column called publication_date should be stored with the value as a date not as a string. You should really fix the data model so the data is stored using the correct types.
Also, you might have a numbers table or calendar table lying around in your database. If so, you can use that instead of the recursive CTE.
I have this query where I calculated cumulative sum. Now, I need to calculate reverse cumulative sum for the same variable
SELECT t1.date, t1.ant, t1.hab,
(#csum:= #csum + t1.hab) as cumulative_hab
from(
SELECT date,
ant,
sum(num_habit) as hab
from xxxxxxxxxx
WHERE date BETWEEN CURDATE() - INTERVAL 5 DAY AND CURDATE()
group by ant) AS t1
,(select #csum := 0) vars
order by t1.ant
My table look like this
date ant hab cumulative_hab
24-05-2020 0 382,000 382,000
24-05-2020 1 28,000 410,000
24-05-2020 2 26,000 436,000
24-05-2020 3 11,000 447,000
24-05-2020 4 29,000 476,000
24-05-2020 6 6,000 482,000
24-05-2020 7 12,000 494,000
28-05-2020 8 50,000 544,000
24-05-2020 12 5,000 549,000
24-05-2020 13 6,000 555,000
I would like another column with reverse running sum (reverse cumulative sum), the first value is calculated 555 - 382
date ant hab cumulative_hab reverse_cum_hab
24-05-2020 0 382,000 382,000 555,000
24-05-2020 1 28,000 410,000 173,000,
24-05-2020 2 26,000 436,000 145,000
24-05-2020 3 11,000 447,000 119,000
24-05-2020 4 29,000 476,000 108,000
24-05-2020 6 6,000 482,000 79,000
24-05-2020 7 12,000 494,000 73,000
28-05-2020 8 50,000 544,000 61,000
24-05-2020 12 5,000 549,000 11,000
24-05-2020 13 6,000 555,000 6,000
As a starter: if you are running MySQL 8.0, you can do this easily with window functions:
select
date,
ant,
sum(num_habit) as hab,
sum(sum(num_habit)) over(order by date) cumulative_hab,
sum(sum(num_habit)) over(order by date desc) reverse_cumulative_hab
from mytable
where date between current_date - interval 5 day and current_date
group by date, ant
order by date
In earlier versions, it is more complicated. I would suggest joining two queries:
select t.*, r.reverse_cumulative_hab
from (
select t.*, #csum := #csum + hab cumulative_hab
from (
select date, ant, sum(num_habit) as hab
from mytable
where date between current_date - interval 5 day and current_date
group by date, ant
order by date
) t
cross join (select #csum := 0) x
) t
inner join (
select t.*, #rcsum := #rcsum + hab reverse_cumulative_hab
from (
select date, ant, sum(num_habit) as hab
from mytable
where date between current_date - interval 5 day and current_date
group by date, ant
order by date desc
) t
cross join (select #rcsum := 0) x
) r on r.date = t.date
order by t.date
This assumes no duplicate ant per date.
It might also be possible to simplify the logic and compute the reverse sum by taking the difference between the cumulative sum and the overall sum:
select t.*, z.total_hab - t.cumulative_hab reverse_cumulative_hab
from (
select t.*, #csum := #csum + hab cumulative_hab
from (
select date, ant, sum(num_habit) as hab
from mytable
where date between current_date - interval 5 day and current_date
group by date, ant
order by date
) t
cross join (select #csum := 0) x
) t
cross join (
select sum(num_habit) as total_hab
from mytable
where date between current_date - interval 5 day and current_date
) z
order by date
Note that these queries are safer than your original code in regard of ordering of the rows: records are ordered in a subquery before the cumulative sum is computed.
I have the following data of a particular user -
Table temp -
time_stamp
2015-07-19 10:52:00
2015-07-18 10:49:00
2015-07-12 10:43:00
2015-06-08 12:32:00
2015-06-07 11:33:00
2015-06-06 10:05:00
2015-06-05 04:17:00
2015-04-14 04:11:00
2014-04-02 23:19:00
So the output for the query should be -
Maximum streak = 4, Current streak = 2
Max streak = 4 because of these -
2015-06-08 12:32:00
2015-06-07 11:33:00
2015-06-06 10:05:00
2015-06-05 04:17:00
And current streak is 2 because of these (Assuming today's date is 2015-07-19)-
2015-07-19 10:52:00
2015-07-18 10:49:00
EDIT: I want a simple SQL query for MYSQL
For MAX streak(streak) you can use this, I have use the same query to calculate max streak. This may help you
SELECT *
FROM (
SELECT t.*, IF(#prev + INTERVAL 1 DAY = t.d, #c := #c + 1, #c := 1) AS streak, #prev := t.d
FROM (
SELECT date AS d, COUNT(*) AS n
FROM table_name
group by date
) AS t
INNER JOIN (SELECT #prev := NULL, #c := 1) AS vars
) AS t
ORDER BY streak DESC LIMIT 1;
A general approach with the gaps and islands queries is to tag each row with its rank in the data and with its rank in the full list of dates. The clusters will all have the same difference.
Caveats: I don't know if this query will be efficient. I don't remember if MySQL allows for scalar subqueries. I didn't look up the way to calculate a day interval in MySQL.
select user_id, max(time_stamp), count(*)
from (
select
t.user_id, t.time_stamp,
(
select count(*)
from T as t2
where t2.user_id = t.user_id and t2.time_stamp <= t.time_stamp
) as rnk,
number of days from t.time_stamp to current_date as days
from T as t
) as data
group by usr_id, days - rnk