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.
Related
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()
I am working in mySQL and I currently have a count of total orders by day, but I would like to add Saturday and Sunday orders to Monday then remove Saturday and Sunday values. I have done some research on this but I cannot seem to find anything similar to what I am trying to do.
My current data table looks like this:
Date | Daily Count
8-6-2020 25
8-7-2020 82
8-8-2020 24
8-9-2020 33
8-10-2020 18
8-11-2020 10
8-12-2020 25
8-13-2020 15
I need it to look something like this:
Date | Daily Count
8-6-2020 25
8-7-2020 82
8-10-2020 75
8-11-2020 10
8-12-2020 25
8-13-2020 15
In this one the Daily counts for the 8th and 9th are added to the 10th, then removed, because they are weekend days. Thank you in advance for your help!
Consider using a case expression to adjust the date:
select
case weekday(date)
when 5 then date + interval 2 day
when 6 then date + interval 1 day
else date
end as new_date,
sum(daily_count) as daily_count
from mytable
group by new_date
I have a table, that is updated every minute and I need to calculate the average value of each hour, for the values of the last 30 days.
Timestamp | SB1_AC_GES_DIFF
2020-07-14 15:13:04 30
2020-07-14 15:12:07 27
... ...
I want to save the results in a second table named avgTable like this
Timestamp | AVG_SB1
15:00 29
16:00 32
... ...
It would be perfect if the table could update itself once a day, maybe when it's 12 o'clock and the the date part for the day changes.
You can try:
INSERT INTO avg_table
SELECT Date_format(Timestamp, "%h:00:00") AS HourlyTimeStamp,
Avg(sb1_ac_ges_diff) AS "AVG_SB1"
FROM table
WHERE Timestamp between DATEADD(DAY, -30, GETDATE()) AND GETDATE()
GROUP BY 1
Assuming that you want the average rolling average, agnostic of the day.
You can just use the hour() function:
select hour(timestamp) as hh, avg(sb1_ac_ges_diff)
from t
group by hh;
You can convert this to a string or time if you want, but that does not seem useful to me.
If you actually want the hour for each day, then:
select date(timestamp) as dd, hour(timestamp) as hh, avg(sb1_ac_ges_diff)
from t
group by dd, hh;
I have a database with CreatedDate is store in Unix epoch time and some other info. I want a query to able to retrieve latest 2 week record base on the last record.
Below is part of the example
ID User Ranking CreatedDate
-------------------------------------------------------
1 B.Sisko 1 1461136714
2 B.Sisko 2 1461123378
3 B.Sisko 3 1461123378
4 B.Sisko 3 1461600137
5 K.Janeway 4 1461602181
6 K.Janeway 4 1461603096
7 J.Picard 4 1461603096
The last record CreatedDate is 25 Apr 2016, so I want the record from 12 Apr to 25 Apr.
I not sure how to compare to get latest data? any suggestion
The simplest method is probably to just subtract two weeks from today's date/time:
where CreatedDate >= UNIX_TIMESTAMP() - 7*24*60*60
Another approach is to convert the value to a date/time:
where from_unixtime(CreatedDate) >= date_sub(now(), interval 2 week)
The advantage of this approach is that it is easier to align to days. So, if you want two weeks starting at midnight:
where from_unixtime(CreatedDate) >= date_sub(curdate(), interval 2 week)
The disadvantage is that the function on the column prevents the use of indices on that column.
EDIT:
This is definitely not how your question was phrased. But in that case, you should use:
select t.*
from t cross join
(select from_unixtime(max(CreatedDate)) as maxcd from t) m
where from_unixtime(CreatedDate) >= date_sub(maxcd, interval 2 week);
It may seem odd, but you need to execute two queries one to find the Maximum Date and knock off 14 days -- and then use that as a condition to requery the table. I used ID_NUM since ID is a reserved word in Oracle and likely other RDBMS as well.
SELECT ID_NUM, USER, RANKING,
TO_DATE('19700101000000', 'YYYYMMDDHH24MISS')+((CreatedDate-18000)
/(60*60*24)) GOOD_DATE
FROM MY_TABLE
WHERE
GOOD_DATE >=
(SELECT MAX( TO_DATE('19700101000000', 'YYYYMMDDHH24MISS')+
((CreatedDate-18000) /(60*60*24))) -14
FROM MY_TABLE)
How can I subtract time in MySQL? For example, today is 16 March; I want to subtract 15 days to reach 1 March. Are there any methods that can be used to subtract 15 days from the current date?
SELECT DATE(NOW()-INTERVAL 15 DAY)
For a list of units see http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add
Not entirely related to this question but is related to the title:
SELECT SUBTIME("10:24:21", "5"); -- subtracts 5 seconds. (returns "10:24:16")
SELECT SUBTIME("10:24:21", "01:00:00"); -- subtracts one hour. (returns "09:24:21")
Documentation: MySQL SUBTIME function
Use:
SELECT NOW() - INTERVAL 15 DAY
to keep the datetime precision.
You can use this :
SELECT DATE(NOW()-INTERVAL 15 DAY);
for when you want to subtract the number of days.
In order to subtract the time instead, say 15 minutes, the following should work:
SELECT(DATE_SUB(NOW(), INTERVAL '15:0' MINUTE_SECOND));
Adding the reference link again :- https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-add.
Yes its possible using date function in Mysql
select distinct
lastname,
changedat, date_add(changedat, interval -15 day) as newdate
from employee_audit;
lastname and changedat is field name and employee_audit is table name.
I have subtract 15 days from my date - check image please. thanks