So, for this query I need to use 2 columns which are date and value. I need to query the average of value over 100 days at a gap of 7 days, in layman terms I want the average value for all 7 days in a week over a time span of 100 days.
Value in my database depicts the revenue. For example, what I am trying to get is the average of revenue over last 100 wednesdays. Similarly for every 7 days in the week.
If I am correct this is the query you are looking for:
SELECT WEEKDAY(RecordDate), AVG(revenue)
FROM Table
GROUP BY WEEKDAY(RecordDate)
WHERE RecordDate BETWEEN DATE_SUB(NOW(), INTERVAL -700 DAY) AND NOW()
Related
I have a monthly offering that gives customers 30 minutes of use every month and I need to tally up their usage and group it by month, but not sure the best way to handle that since the start date could be any day of the month.
Should I prorate? Convert into full months or count the days?
Is there an ideal way to tally this? Let's say each month they get 30 minutes of use time, so I want to tally how many minutes were used that "month."
Say there is a minutes used table and the start date for the subscription is 2019-06-13 00:00:00
minutes_used_table:
Userid MinutesUsed Date
1 5 2019-06-19
1 6 2019-06-23
1 8 2019-06-28
1 15 2019-07-05
1 3 2019-07-12
1 8 2019-07-19
1 5 2019-08-14
1 3 2019-08-22
1 1 2019-08-26
1 2 2019-09-13
Or, should I prorate it and instead of tracking in 30 day increments, if they start on the 13th of June, should I just count the days from the start date to the end of the month, whatever day that is and then the days from the first of the month until the start date?
Wondering what makes the most sense and how to carry it out?
Use
GROUP BY DATEDIFF(`Date`, #starting_date) DIV #period_length
In your particular case it will be
GROUP BY DATEDIFF(`Date`, '2019-06-13 00:00:00') DIV 30
From coding point of view it's not a big deal to find first date for user, subtract it from dates and count number of 30-days periods.
But this will be very hard to support in future and this solution will have a number of corner cases: for example someone first start to use product at 20 January, after month of usage have been idle for a while and came back in April, 5. What will be right start date for that case?
So I suggest to use calendar months. And may be reduce limit on first month accordingly to the number of days left or give full trial period even on first month as user has ability to spend 30 minutes.
I have a table that contains 2 columns : 1 column "app_id" and 1 column "time".
I am trying to make a SQL request to know the number of "app_id" that have been used at least once in 3 different days, in the last 7 days.
Currently, I achieved selecting all the data in the last 7 days using :
SELECT app_id,time FROM connexions WHERE time BETWEEN NOW() - INTERVAL 167 HOUR AND NOW()
I am using 167 HOUR instead of 7 DAY because I have a 1 hour time difference between my server and the database (no worries about that, i'll fix it later!)
Thanks!
SELECT app_id
FROM connexions
WHERE time BETWEEN NOW() - INTERVAL 167 HOUR AND NOW()
GROUP BY app_id
HAVING COUNT( DISTINCT day(time) ) > 3
Be aware this only works because is a week. If you want something like 3 months you would need be more specific.
I've got a MySQL database filled with weather data, e.g. mean temperature value for every day. I would like query for the average of these values for every day the last five years.
for example:
2019-06-04 20.04
2018-06-04 18.42
2017-06-04 19.21
2016-06-04 21.22
2015-06-04 17.19
query result should be: 19.216
For now I am able to get the avg for a specific day for the last years:
select date, avg(ta) from weatherdata where date like "20%-06-04";
But I am searching for an option to get the avg value for every day in a single query if possible.
Use GROUP BY.
SELECT MONTH(date) AS month, DAY(date) AS day, AVG(ta)
FROM weatherdata
GROUP BY month, day
ORDER BY month, day
So what I want to achieve is to return data from within the last 3 months.
My current MySQL query is as follows:
SELECT MONTH(service_date_time) AS month, SUM(service_price) AS total FROM appointments WHERE user_id = 1 AND service_date_time >= last_day(now()) + INTERVAL 1 day - INTERVAL 3 month GROUP BY YEAR(service_date_time), MONTH(service_date_time)
My table appointments contains data from January and May and service_date_time is a date_time field.
The problem I'm having is that it returns 2 rows, one row totaling the price for January and one row totaling the price for May. The row for May shouldn't be returned since it is not within the past three months.
Anyone have an idea why?
You are requesting all records that are greater than the given date, If you want all the records up to now you'll have to ask for a range for example:
WHERE service_date_time BETWEEN (LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 3 MONTH) AND NOW()
this would limit the records & give you from 3 months ago till now
I have statistical data like this:
time val1
1424166578 51
1424166877 55
1424167178 57
1424167477 57
time is a unix timestamp. There is one record every 5 minutes excluding nights and sundays. This continues over several weeks.
Now I want to get these values for an average day and an average week. The result should include values for every 5 minutes like normal but for average past days or weeks.
The result should look like this:
time val1
0 43.423
300 46.635
600 51.887
...
So time could be a timestamp with relative time since day or week start. Perhaps it is better to use DATETIME... not sure.
If I use GROUP BY FROM_UNIXTIME(time, '%Y%m%d') for example I get one value for the whole day. But I want all average values for all days.
You seem to be interested in grouping dates by five minute intervals instead of dates. This is fairly straightforward:
SELECT
HOUR(FROM_UNIXTIME(time)) AS HH,
(MINUTE(FROM_UNIXTIME(time)) DIV 5) * 5 AS MM,
AVG(val1) AS VAL
FROM your_table
WHERE time > UNIX_TIMESTAMP(CURRENT_TIMESTAMP - INTERVAL 7 DAY)
GROUP BY HH, MM
The following result will explain how date is clamped:
time FROM_UNIXTIME(time) HH MM
1424166578 2015-02-17 14:49:38 14 45
1424166877 2015-02-17 14:54:37 14 50
1424167178 2015-02-17 14:59:38 14 55
1424167477 2015-02-17 15:04:37 15 00
I would approach this as:
select date(from_unixtime(time)) as day, avg(val)
from table t
group by date(from_unixtime(time))
order by day;
Although you can use the format argument, I think of that more for converting the value to a string than to a date/time.