I'm trying to find the number of orders that were open for each week. An order that is open for multiple weeks should be included in each week's count that it was open. The data looks something like below
id open_dt close_dt
1 2014-01-01 07:00:00 2014-01-01 07:00:00
2 2014-01-01 07:00:00 2014-01-02 07:00:00
3 2014-01-02 07:00:00 2014-01-09 07:00:00
4 2014-01-08 07:00:00 NULL
NULL close_dt counts as still open and should appear in each week since it was opened
My query looks like below however it isn't returning the numbers I'm expecting:
SELECT YEAR(open_dt) AS year, WEEK(open_dt) AS week, count(*) 'open'
FROM table
WHERE open_dt >= week(open_dt)
OR
(
close_dt > week(open_dt)
OR close_dt IS NULL
)
GROUP BY YEAR(open_dt), WEEK(open_dt)
I'm trying to get results like below:
year week open
2014 1 3
2014 2 2
2014 3 1
...
Appreciate any tips or guidance.
This is a case where it helps to have a calendar table or list of weeks. Let me assume that you have at least one open in each week:
select yw.y, yw.w, count(t.open_dt) as "Open"
from (select distinct year(open_dt) as y, week(open_dt) as w,
year(open_dt) * 100 + week(open_dt) as yw
from table t
) yw left outer join
table t
on yw.yw >= year(open_dt)*100 + week(open_dt) and
(yw.yw <= year(close_dt)*100 + week(close_dt) or close_dt is null)
group by yw.y, yw.w
order by yw.y, yw.w;
Related
I have a MySQL requirement to select data from a table based on a start date and end date and group it by weekly also selecting the data in reverse order by date. Assume that, I have chosen the start date as 1st November and the end date as 04 December. Now, I would like to fetch the data as 04 December to 28 November, 27 November to 20 November, 19 November to 12 November and so on and sum the value count for that week.
Given an example table,
id
value
created_at
1
10
2021-10-11
2
13
2021-10-17
3
11
2021-10-25
4
8
2021-11-01
5
1
2021-11-10
6
4
2021-11-18
7
34
2021-11-25
8
17
2021-12-04
Now the result should be like 2021-12-04 to 2021-11-28 as one week, following the same in reverse order and summing the column value data for that week. I have tried in the query to add the interval of 7 days after the end date but it didn't work.
SELECT count(value) AS total, MIN(R.created_at)
FROM data_table AS D
WHERE D.created_at BETWEEN '2021-11-01' AND '2021-12-04' - INTERVAL 7 DAY ORDER BY D.created_at;
And it's also possible to have the last week may have lesser than 7 days.
Expected output:
end_interval
start_interval
total
2021-12-04
2021-11-27
17
2021-11-27
2021-11-20
34
2021-11-20
2021-11-13
4
2021-11-13
2021-11-06
1
2021-11-06
2021-10-30
8
2021-10-30
2021-10-25
11
Note that the last week is only 5 days depending upon the selected from and end dates.
One option to address this problem is to
generate a calendar of all your intervals, beginning from last date till first date, with a split of your choice, using a recursive query
joining back the calendar with the original table
capping start_interval at your start_date value
aggregating values for each interval
You can have three variables to be set, to customize your date intervals and position:
SET #start_date = DATE('2021-10-25');
SET #end_date = DATE('2021-12-04');
SET #interval_days = 7;
Then use the following query, as already described:
WITH RECURSIVE cte AS (
SELECT #end_date AS end_interval,
DATE_SUB(#end_date, INTERVAL #interval_days DAY) AS start_interval
UNION ALL
SELECT start_interval AS end_interval,
GREATEST(DATE(#start_date), DATE_SUB(start_interval, INTERVAL #interval_days DAY)) AS start_interval
FROM cte
WHERE start_interval > #start_date
)
SELECT end_interval, start_interval, SUM(_value) AS total
FROM cte
LEFT JOIN tab
ON tab.created_at BETWEEN start_interval AND end_interval
GROUP BY end_interval, start_interval
Check the demo here.
update: this can be done with python. here
i have a table like this:
event_id vendor_id start_date end_date
1 100 2021-01-01 2021-01-31
2 101 2021-01-15 2021-02-15
3 102 2021-02-01 2021-02-31
4 103 2021-02-01 2021-03-31
5 104 2021-03-01 2021-03-31
6 105 2021-03-01 2021-04-31
7 100 2021-04-01 2021-04-31
i would like an output like this: number of events based on month. but if the event between two or more months, it must be included in the count for each month. For example, The event in the second row (event_id=2) takes place in both January and February. Therefore, this event should be included in the total both in January and February.
output:
month total_event
2021-01 2 ---->> event_id=(1,2)
2021-02 3 ---->> event_id=(2,3,4)
2021-03 3 ---->> event_id=(4,5,6)
2021-04 2 ---->> event_id=(6,7)
Note: I wrote it to make the " --->> event_id= : " part better understood. i dont needed. i just need the month and the total_event.
i tried this query:
select date_format(start_date,'%Y-%m') as month,count(event_id) as total_event
group by date_format(start_date,'%Y-%m')
month total_event
2021-01 2
2021-02 2
2021-03 2
2021-04 1
but it counts only by start_date, so the numbers are missing.
Idea
To get the valid months list from the table
To calculate the event counts by event table's joining with the months
MySQL 8.0+
We can get the valid months list by Recursive.
Here is a full SQL. Assumed that your event table is c!
WITH RECURSIVE all_dates(dt) AS (
-- anchor
SELECT MIN(c.`start_date`) AS dt FROM c
UNION ALL
-- recursion with stop condition
SELECT dt + INTERVAL 1 MONTH
FROM all_dates WHERE dt + INTERVAL 1 MONTH <= (SELECT MAX(c.end_date) FROM c)
)
SELECT LEFT(dt, 7) AS `month`, COUNT(d.dt) AS total_event, GROUP_CONCAT(DISTINCT c.`event_id`) AS event_ids FROM all_dates d
INNER JOIN c ON LEFT(d.dt, 7) >= LEFT(c.start_date, 7) AND LEFT(d.dt, 7) <= LEFT(c.end_date, 7)
GROUP BY LEFT(dt, 7);
Hi I'm trying to count order records with users who made more than one order in a month like 2018-01-01 to 2018-02-01.
Order table
id user_id date
1 12 2017-01-02 <- first order(no count)
2 23 2018-01-03 <- second order(count)
3 12 2018-01-04 <- second order(count)
4 12 2018-01-08 <- third order(count)
5 23 2017-11-02 <- first order(no count)
6 11 2018-01-01 <- first order(no count)
....
User table
id
11
12
23
....
Output
date count(*)
2018-01-01 3
I think I need to get order records first and find order records again with certain user_id. But I'm stuck
Is there a way to accomplish this task?
Thanks
How about this:
select count(*) from
(select user_id, count(*)
from `Order`
where date >= '2018-01-01' and date < '2018-02-01'
group by user_id
having count(*) > 1) users_w_multiple_orders;
The having command is how you filter results from an aggregation like sum. After you have that, you can count the results from that query.
I'm trying to create a query that will return totals of a number based on every week so I can create a rising trend line chart. In my table I have a number of records that record a completion date (completed). I'd like to be able to create a query that generates a rolling total every week. So if week 1 there are 10 completed, week 2 there are 15 completed, and week 3 has 5 completed the desired result would be:
Week 1 totals: 10
Week 2 totals: 25
Week 3 Totals: 30
Sample data:
id status sched
12 Successful 2017-04-04 00:00:00.000
15 Successful 2017-06-20 19:30:00.000
18 Successful 2017-10-17 18:00:00.000
26 Successful 2017-04-05 00:00:00.000
29 Successful 2017-06-16 00:00:00.000
30 Successful 2017-04-06 00:00:00.000
31 Successful 2017-04-07 00:00:00.000
32 Successful 2017-04-06 00:00:00.000
34 Successful 2017-10-18 18:00:00.000
35 Successful 2017-06-13 00:00:00.000
This is the query I'm using to successfully generate data BY WEEK without any rollups. I tried adding "WITH ROLLUP" but it only gave the grand total at the end, not week by week.
select DATE_FORMAT(completed,'%d/%m/%Y') AS nd , wk, count(*)as totals
from
(
select id, completed, yearweek(completed)as wk from w10_upgrades
where status = 'Successful' and type = 'Normal'
and yearweek(completed) is not null
) x
GROUP BY wk
ORDER BY wk;
Desired output:
wk totals
201714 10
201715 25 (output would = week 201714 + 201715)
201716 55 (output would = week 201714 + 201715 + 201716)
ect...
Any direction is appreciated. I can't find anything related to this.
Final result
SET #runtot:=0;
SELECT
DATE_FORMAT(completed,'%d/%m/%Y') AS niceDate,
wk,
(#runtot := #runtot + c) AS rt
FROM
(SELECT
completed,
yearweek(completed)as wk,
COUNT(*) AS c
FROM `w10_upgrades`
where status = 'Successful' and type = 'Normal'
and yearweek(completed) is not null
GROUP BY wk
ORDER BY wk) x
I have the following table (simplified):
user_id date hours
1 2012-03-01 5
2 2012-03-01 8
3 2012-03-01 6
1 2012-03-02 3
3 2012-03-02 7
What I want is to get the the sum of hours worked for a given user id (ex. 1), and the total hours worked regardless of what user (for a given time period) in a single query.
So for user_id = 1, and time period: 2012-03-01 - 2012-03-02 the query should return: own=8, total=29.
I can do it in two separate queries, but not in a single one.
Use CASE:
SELECT SUM(
CASE user_id
WHEN 1 THEN hours
ELSE 0
END) as Own,
SUM(hours) as Total
FROM HoursWorked
WHERE date BETWEEN '2012-03-01' AND '2012-03-02';
I think I have something that works using the following schema:
CREATE TABLE hoursWorked
(
id int,
date date,
hours int
);
INSERT INTO hoursWorked
(id, date, hours)
VALUES
('1','2012-03-01','5'),
('2','2012-03-01','8'),
('3','2012-03-01','6'),
('1','2012-03-02','3'),
('3','2012-03-02','7');
And this query:
select parent.id, parent.date, parent.hours, (select sum(hours)
from hoursWorked child
where child.id = parent.id) as totalHours
from hoursWorked parent
I was able to get these results:
ID DATE HOURS TOTALHOURS
1 March, 01 2012 00:00:00-0800 5 8
2 March, 01 2012 00:00:00-0800 8 8
3 March, 01 2012 00:00:00-0800 6 13
1 March, 02 2012 00:00:00-0800 3 8
3 March, 02 2012 00:00:00-0800 7 13
Diego's answer albeit procedural is a great way to get the answer you are looking for. Of course for your date range you would need to add a WHERE date BETWEEN 'startdate' AND 'enddate'. The dates need to be in a format that mysql recognizes, typically 'yyyy-mm-dd'
Another solution that doesn't get you the results in one row, but in a result set would be to do a UNION
SELECT user_id, SUM(hours) as hours FROM table WHERE date BETWEEN 'startdate' AND 'enddate' WHERE user_id = 3
UNION
SELECT null as user_id, SUM(hours) as hours FROM table WHERE date BETWEEN 'startdate' AND 'enddate'