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
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.
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())
I have a table consisting of stock market's daily data. It consists of various number of rows for each day.I have a column named 'High' in the table. Now I need to calculate MAX(High) for last 3 week days. For ex:- if today is Wednesday I need to calculate MAX of last week's friday and this week's monday,tuesday. I know that I can do this if I know the date manually using a query like this.
Select MAX(HIGH) from table_name where date>='date'
But I don't want to do like this i just want to automate this with a program written in PHP. How can i Achieve this any help in both either PHP or SQL is appreciable. My table just has 6 columns
date,time,open,high,low,close
say suppose if my table is like this
date time open high low close
2015-05-06 09:30:00 2012.50 2020.5 2016.5 2014.0
2015-05-06 09:31:00 2013.50 2021.5 2014.5 2016.0
2015-05-06 09:32:00 2014.50 2021.75 2017.5 2013.0
2015-05-07 09:30:00 2011.50 2019.5 2018.5 2014.0
2015-05-07 09:31:00 2014.50 2022.5 2016.5 2015.0
2015-05-07 09:32:00 2012.50 2026.5 2017.5 2016.0
2015-05-08 09:30:00 2010.50 2024.5 2015.5 2017.0
2015-05-08 09:31:00 2013.50 2026.5 2017.5 2018.0
2015-05-08 09:32:00 2014.50 2028.5 2015.5 2019.0
2015-05-08 09:33:00 2014.50 2022.5 2017.5 2012.0
2015-05-11 09:30:00 2017.50 2025.5 2018.5 2013.0
2015-05-11 09:31:00 2018.50 2027.5 2019.5 2016.0
2015-05-11 09:32:00 2019.50 2024.5 2011.5 2017.0
2015-05-11 09:33:00 2020.50 2026.5 2017.5 2014.0
2015-05-12 09:30:00 2018.50 2023.5 2018.5 2018.0
2015-05-12 09:31:00 2017.50 2024.5 2017.5 2014.0
2015-05-12 09:32:00 2018.50 2023.5 2018.5 2013.0
2015-05-12 09:33:00 2017.50 2024.5 2019.5 2014.0
2015-05-12 09:34:00 2016.50 2023.5 2016.5 2012.0
2015-05-12 09:35:00 2017.50 2025.5 2018.5 2011.0
and if today's date is 2015-05-13(wednesday) I need MAX(high) of last 3 week days i.e 2015-05-12,11,08 which is 2028.5.
Presumably, you only have data on "week days".
select max(high)
from table_name t join
(select date
from table_name
group by date
order by date desc
limit 3
) dates
on t.date = dates.date;
Presumably, you either want a condition on the stock or a group by, but this is based on your sample query.
You can make this more efficient by adding a where clause. Typically, the last three working days would be within the last week, or so:
select max(high)
from table_name t join
(select date
from table_name
where date >= date_sub(curdate(), interval 7 day)
group by date
order by date desc
limit 3
) dates
on t.date = dates.date;
You can use date_sub function for this case:
select max(high) from table_name where date > date_sub(date, INTERVAL 3 DAY);
I think this is the correct query:
select top 3 date, max(high) from table_name order by date desc
I have a table - user_tracking - which stores the user_id, purchase sku, and event time_created. Each time a user returns to purchase the original user_id is referenced with a new timestamp:
User_ID Sku Time_Created
1 1234 2012-10-01 01:00:00
2 2345 2012-10-02 02:00:00
3 6789 2012-10-02 01:00:00
2 5432 2012-10-04 04:00:00
I want to measure the return customer usage, but only for customers that have returned within 7-60 days of initial purchase. Currently my query looks something like:
SELECT
total_purchases.user_id as user_1_id,
total_purchases.time_created as time_1_created,
total_purchases.total_purchases as total_original_purchases,
total_return.user_id as user_2_id,
total_return.time_created as time_2_created,
total_return.total_return_purchases as total_return_purchases
FROM (SELECT
user_tracking.user_id as user_id,
user_tracking.time_created as time_created,
COUNT(DISTINCT user_tracking.sku) as total_purchases
FROM user_tracking
WHERE user_tracking.time_created BETWEEN "2012-10-01 00:00:00"
AND "2012-10-15 00:00:00") AS total_purchases
LEFT JOIN (SELECT
user_tracking.user_id as user_id,
user_tracking.time_created as time_created,
COUNT(DISTINCT user_tracking.sku) as total_return_purchases
FROM user_tracking
WHERE user_tracking.time_created BETWEEN "2012-10-01 00:00:00"
and "2012-12-15 00:00:00") AS total_return
ON total_purchases.user_id = total_return.user_id
How can I ensure I'm only measuring purchases within 7-60 days with the original user?
You can use interval
AND datecolumn BETWEEN (datecolumn, INTERVAL 7 DAYS) AND (datecolumn, INTERVAL 60 DAYS)
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`)