I have bellow snippet table format.
Table name:- wp_lead_count_freevendor
id entryid start_date end_date user_id count_set entry_date cancel_set_date
70 11392 2015-12-03 2015-12-03 3185 1 2015-12-03 2015-12-04
71 11393 2015-12-03 2015-12-03 3185 1 2015-12-03 2015-12-04
72 11394 2014-10-01 2014-10-01 3185 1 2014-10-01 2014-10-01
Here i want to calculate count total count_set column. of current month & year by start_date column WHERE user_id=3185.
suppose in start_date current year is 2015 & current month is 12:-
year month count total
2015 12 2
For user id 3185 of this month of year count_set total =2
so any body will tell how do i fire the query to get count_set total for current year of current month for user_id=3185.
I have tried bellow query but its not working.
$check_per_month=mysql_query("SELECT DATE_FORMAT(end_date, '%Y') as 'year',
DATE_FORMAT(end_date, '%m') as 'month',
COUNT(id) as 'total'
FROM wp_lead_count_freevendor WHERE user_id=$wp_lead_count_user_id
GROUP BY DATE_FORMAT(end_date, '%Y%m')") OR DIE(mysql_error());
while($row = mysql_fetch_assoc($check_per_month))
{
echo $sql_chk_current_month_count=$row['total'];
}
Try to count using the where , year and month methods like this:
SELECT .... WHERE YEAR(start_date)=2015 AND MONTH(start_date)=12
Try this query:
SELECT YEAR(NOW()) as `year`,MONTH(NOW()) as `month', SUM(count_set) as `count_set`
From wp_lead_count_freevendor
WHERE YEAR(start_date)=YEAR(NOW()) AND MONTH(start_date)=MONTH(NOW()) AND user_id=3185
group by YEAR(NOW()),MONTH(NOW())
Related
How can I get the average price on Monday grouped by time (every hour)
Table looks like:
date
price
2023-01-15 23:00:00
0.13
2023-01-15 22:00:00
0.18
2023-01-15 21:00:00
0.16
2023-01-15 20:00:00
0.20
....
2022-01-01 00:00:00
0.15
I tried query below:
SELECT date, AVG(price) as 'Monday'
FROM table
WHERE YEAR(date) = '2022' AND WEEKDAY(date) = 1
GROUP BY date_format(date, '%HH:%mm')
But it returns only the first monday of each month.
Expected result should be like this (probably I have to create 7 queries, each for one weekday):
Expected result
This should do the trick:
SELECT DATE_FORMAT(MIN(`date`), "%Y-%m-%d"), HOUR(`date`), AVG(`price`) as 'Monday'
FROM `table`
WHERE YEAR(`datetime`) = "2023" AND WEEKDAY(`datetime`) = 0
GROUP BY HOUR(`datetime`);
https://www.db-fiddle.com/f/gWMeq6hkn8oVqmwz9ryF9J/0
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);
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;
Given the following sample data:
tblData
Date Sales
----------------------
2011-12-01 122
2011-12-02 433
2011-12-03 213
...
2011-12-31 235
2011-11-01 122
2011-11-02 433
2011-11-03 213
...
2011-11-30 235
2011-10-10 122
2011-10-11 433
2011-10-12 213
...
2011-10-31 235
Notice that October data begins at 10 October, whereas subsequent months have complete data.
I need to get the average monthly sales over all complete months, which in this case would be November and December 2011.
How would I do this?
SELECT `date`, AVG(`sales`)
FROM sales
GROUP BY YEAR(`date`), MONTH(`date`)
HAVING COUNT(`date`) = DAY(LAST_DAY(`date`));
Example
If you want to limit the result, either
HAVING ...
ORDER BY `date` DESC LIMIT 3
which should always return data for the 3 most recent months, or something like
FROM ...
WHERE DATE_FORMAT(CURDATE() - INTERVAL 3 MONTH, '%Y-%m')
<= DATE_FORMAT(`date`, '%Y-%m')
GROUP BY ...
which should return data for the 3 previous months, if there is any. I'm not sure which is better but I don't believe WHERE gets to use any index on date, and if you're using DATETIME and don't format it you'll also be comparing the days and you don't want that,
Can't test it right now, but please have a try with this one:
SELECT
DATE_FORMAT(`Date`, '%Y-%m') AS yearMonth,
SUM(Sales)
FROM
yourTable
GROUP BY
yearMonth
HAVING
COUNT(*) = DAY(LAST_DAY(`Date`)