I'm currently running the following query to get the daily average of entries per user on my database, it's working as expected but I want to modify it to get the 7 day averages by day.
SELECT
AVG(bg),
AVG(carbs),
timestamp
FROM users_entries
WHERE uid = '10b47fded7d2ea8d' AND
timestamp >= '2019-01-01 00:00:00' AND timestamp <= '2019-01-30 00:00:00'
GROUP BY DAY(timestamp)
So for example, for the time frame, say 2019-01-01 00:00:00 to 2019-06-01 00:00:00 I would like to find all averages for 7 days and list them out. Basically take each day in the time frame, go back 7 days and get the average of the columns I select.
I'm thinking that this would require some sort of subquery but based on what I see online I do not understand them well enough to figure it out on my own, any help would be great.
In MySQL 8+, you can use window functions:
SELECT DATE(timestamp),
AVG(bg),
AVG(carbs),
AVG(AVG(bg)) OVER (ORDER BY DATE(timestamp) ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) as bg_7,
AVG(AVG(carbs)) OVER (ORDER BY DATE(timestamp) ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) as bg_7,
FROM users_entries
WHERE uid = '10b47fded7d2ea8d' AND
timestamp >= '2019-01-01' AND
timestamp < '2019-01-30'
GROUP BY DATE(timestamp);
This is much more challenging in older versions of MySQL.
Related
Assuming that there are 6 months of historical data with hundreds of rides per day:
Write a query that returns, for each of the last 90 days, a count of the rides taken in the 7 day window preceding that day
I would like to find a way to write this in MySQL but have had some trouble with having a rolling sum that resets along with how I could cut up timestamps to reflect a day of the year/date and to then group by that.
I have tried writing subqueries that will limit the sum to a week prior and then place an additional limit of 90 days after that but cannot seem to get the code to return any output.
I have tried writing this is PostgreSQL using a sort of "window" functionality but am much more comfortable working in MySQL and would like to be able to solve it that way. I am familiar on how to write limits, group and order among other things but I am having trouble with the rolling sum resetting per week.
Thank you for your help!
First you'll want a numbers table/query. There are some tricky CTE ways to do that but it might be easier for now just to add a table with the numbers 1-90 in 90 rows.
Then use that to generate, for each row, a date range. Sorry if the syntax isn't quite correct, but write a query along the lines:
SELECT num, DATE_ADD(CURRENT_DATE(), INTERVAL -(num+7) DAY) startdate, DATE_ADD(CURRENT_DATE(), INTERVAL -num DAY) enddate FROM numbers
Then you can cross-join that with your rides table grouped on num and counting the rows in the range:
SELECT num, startdate, enddate, SUM(CASE WHEN startdate <= ridedate AND ridedate <=
enddate THEN 1 ELSE 0 END) ridecount
FROM (date range query) dts, rides
GROUP BY dts.num
Hope that helps.
Assuming you have data on each day, a correlated subquery might be simplest approach:
select dt,
(select count(*)
from rides r
where r.ridedate >= d.dte - interval 7 day and
r.ridedate < d.date
) as rolling_7
from (select distinct ridedate as dt
from rides
) dt
In MySQL, it is fairly easy to find the number of records that exist within some time interval.
SELECT COUNT(*) FROM records WHERE create_date > '2018-01-01 01:15:00' AND create_date < '2018-01-01 02:15:00'
But I want to do the opposite, sort of. Rather than providing a time interval and getting a count of records, I want to provide a count of records and check if a X minute time interval exists where more than Y many records were created. Getting the exact time interval is not essential, only if one exists or not. At a higher level, I am attempting to identify if there was any X minute "surge" when more than Y records where created during the course of a day.
For example, in the past 24 hours was there any 1 hour interval where a "surge" of more than 50 new records occurred?
I have already ruled out dividing the 24 hours into blocks of 1 hour intervals and checking each block. This does not work because the "surge" could span two sequential 1 hour blocks, such as 25 records at the end of the 01:00:00 block and 25 records at the beginning of the 02:00:00 block.
This should do it:
SELECT COUNT(*)
FROM records r1
WHERE
(SELECT COUNT(*) FROM records r2
WHERE ABS(UNIX_TIMESTAMP(r1.create_date) - UNIX_TIMESTAMP(r2.create_date)) < X) > Y
What this does is count how many records have more than Y records that have been created within X seconds after or before each record.
So basically it will return >=1 if there are any, 0 if not.
So if you wanted to sort by hours you would want to group the records. Here I'm using the built-in functions that return parts of a timestamp, year(), month(), dayofmonth(), hour(). Since you can't use an aggregate function in the where clause I had to use having to limit by the count requirement.
select date(create_date),
hour(create_date),
count(*) as surge from records
where create_date > curdate() - interval 1 day
group by year(create_date), month(create_date),
dayofmonth(create_date), hour(create_date)
having count(*) > 50;
Another method to accomplish your goal might be to select the count of records and group by the interval in question. In this case I'm adding an hour to the create_date to get your 1 hour suggested interval. Anytime the count is greater than 50 it returns a row. Notice I'm also grouping by the hour. This is to prevent multiple starts for a "surge" within the same hour:
select create_date,count(*) as surge from records
group by year(create_date), month(create_date),
dayofmonth(create_date),hour(create_date),
(create_date + interval 1 hour - create_date) having count(*) > 50;
The problem with this however is that some surges may last longer than 1 hour, but it should give you the moment the "surge" started.
I am writing annual membership registrations to a single db table. I need to keep track of when renewals have occurred in less than 11 months from their last renewal.
I look for the duplicate rows based on multiple criteria. I currently have this working with out the 11 month criteria, although it's slow. Here's what I currently use.
SELECT y_reg.* FROM y_reg WHERE (((y_reg.season) In (SELECT season FROM y_reg As Tmp
GROUP BY season, Father_Last_Name, Father_First_Name
HAVING Count(*)>1
AND Father_Last_Name = y_reg.Father_Last_Name
AND Father_First_Name = y_reg.Father_First_Name)))
ORDER BY y_reg.season, y_reg.Father_Last_Name, y_reg.Father_First_Name
I have a field Date which is the date of the renewal that I need to evaluate. I'd like to add something like "AND Date - Date < 335"
335 is the number of days and is about 1 month short of a year. But I just keep getting syntax error because I clearly don't know what I'm doing.
Date arithmetic works quite well in MySQL; you just need the knack.
You can say things like
AND later.Date >= earlier.Date
AND later.Date < earlier.Date + INTERVAL 11 MONTH
That particular pair of comparisons comes up true if the later date occurs in the time range between the earlier date and 11 months later.
In general you can say stuff like this to do date arithmetic.
datestamp + INTERVAL 1 HOUR
datestamp - INTERVAL 5 MINUTE
datestamp + 1 MONTH - 3 WEEK
datestamp - INTERVAL 3 QUARTER (calendar quarters)
LAST_DAY(datestamp) + INTERVAL 1 DAY - INTERVAL 1 MONTH
The last item is the first day of the month containing the datestamp. This whole date thing works quite well.
I think you should consider a so-called self-join query to get your duplicate-except-for-date results. Try something like this.
SELECT a.*
FROM y_reg a
JOIN y_reg b ON a.Father_Last_Name = b.Father_Last_Name
AND a.Father_First_Name = b.Father_First_Name
AND b.Date < a.Date - 11 MONTH
AND b.Date >= a.Date - 12 MONTH
I'm attempting to create a select statement which gets items by year and month.
This is what I have so far:
SELECT * FROM sales WHERE YEAR(Date) = 2013 AND MONTH(?) = 'June'
I can't merely select date ranges because different months have different number days. It would be ideal to select months by their number (ie, January being 1) or a similar approach.
How is this worked out in a mysql statement?
The fields are datetime fields such as 2012-12-01 00:00:00
Have a look at the performance and write your condition as
SELECT * FROM sales
WHERE
Date >= '2013-06-01 00:00:00'
AND
Date < '2013-07-01 00:00:00'
for the example month: June of 2013
so MySQL can use an index on the column date. You will get exactly all rows with a date in the June of 2013, even those in the first second, but not those in the first second of the July of 2013.
You see, that you don't need to know the number of days of the particular month, because you will ever use the first of both months.
You could use a bit of date calculation too:
SELECT * FROM sales
WHERE
Date >= '2013-06-01 00:00:00'
AND
Date < '2013-06-01 00:00:00' + INTERVAL 1 MONTH
so you need only to know the start and the length of the interval.
Note
The most important part of my answer is to use the column Date without using a function on this column, so MySQL can use an index.
I have a table from which I'm trying to extracted summed timediff information grouped by days. I don't really know if this is possible
Table columns: mode_type, start_time.
A record exists in this table for each time an employee starts or stops a timer. mode_type = 1 for start, mode_type = 0 for stop.
I'd like to return a sum of the seconds used for each day in the last 30 days.
E.g:
date, seconds_used
02/04/2014, 25
03/04/2014, 12415
04/04/2014, 925
Currently I can return a list of seconds used per mode_type and date but this required later calc in PHP.
SELECT
mode_type,
Sum(Unix_Timestamp(start_time)) AS time,
start_time
FROM
activations
WHERE
start_time < Date(Now() + INTERVAL 1 MONTH)
GROUP BY
mode_type, Day(start_time)
ORDER BY
start_time
I'm stuck... is this possible or do I need to do revert to calculating the diff in PHP post request?
Thanks in advance.
Can you try with this:
SELECT DATE(start_time) AS startdate, TIME_TO_SEC(TIMEDIFF(NOW(),start_time)) AS secs
FROM activations
GROUP BY
startdate