I have a table within MySQL which is being used to store user session information across a fleet of instances. The structure of the table is pretty simple, with 6 fields.
Item: 37824
Timestamp: 2014-10-30 23:01:41
Active: 14
Idle: 2
Total: 16
Server:
The metrics are collected every 5 minutes.
I have a query which pulls the SUM of one of the metrics for the past 5 minutes.
select SUM(metric_activeSessions) from metrics_tbl WHERE metric_timeStamp > NOW() - INTERVAL 5 MINUTE
I want to build a chart from these metrics, using one of the many great libraries available, but yet to be determined.
I would like the chart to show the SUM of a given metric every over a period of 60 minutes, at 5 minute intervals.
- 09:00 : 12
- 09:05 : 15
- 09:10 : 18
- 09:15 : 21
- 09:20 : 28
I have no idea how to write an SQL query to collect information in that way and was hoping someone out there may be able to help me out.
Thank you in advance for any ideas,
Cheers,
Mitch
Assuming metric_timeStamp is formatted as a mysql timestamp, try something like this..
SELECT SUM(metric_activeSessions), MAX(metric_timeStamp) as max_time, MIN(metric_timeStamp) as min_time,
FROM metrics_tbl
WHERE metric_timeStamp > NOW() - INTERVAL 60 MINUTE
GROUP BY unix_timestamp(metric_timeStamp) div 300
Use that as a starting point. I'm eating ice cream right now so I can't run this myself, but that's the general idea.
Edit: fixed syntax issue
Edit 2: I was grouping by 10 minutes. Changed it to 5 minutes
Edit 3: If your rows contain a running total, then change
SUM(metric_activeSessions)
to
MAX(metric_activeSessions)
in the query above.
Related
I want to represent the time occurrence of an event on a chart.
I'm not familiar with javascript.
Based on the zoom, it could be the number of occurrence for each seconds, or minutes, or hours, or days, ...
I'm using a nodeJS express website, an mySQL database where I have the timestamp of all my events.
A sample of the database:
The Start and Stop period could be severals days/weeks.
I would like to represent the number of disconnection on a chart.
With no zoom, I might get the number of disconnection for each day, by zooming, I might get the number of disconnection for each hours, more each minutes and more each seconds. 0 should be displayed when there is no events.
In the same way as:
https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/demo/line-time-series/
Before, for a solution which is not dynamic, I've used a mysql query to get the count for every minutes:
select DATE_FORMAT(time, '%Y-%m-%d %H:%i') as date, count(*) as Count
from db
WHERE event = "disconnected"
group by date
But the result is:
2019-09-18 00:36 4
2019-09-18 18:14 2
2019-10-02 11:43 2
2019-10-02 11:44 1
I would need to get every minutes with 0 when there is not events to get a correct barchart.
Right now, I have this display:
https://jsfiddle.net/pe3ua4t5/3/
I'm new for mysql, Already value in time field, I want to update extra 5 minutes in time field using query. I tried so many things but not working.
Here my query:
UPDATE STUDENT SET START_TIME = ADDTIME(START_TIME, 500) WHERE ID = 1;
Above query working but one issue is there that is, If my field having 23:55:00.
I want result after executing query 00:00:00 but it updates 24:00:00.
Anyone help me!
Thanks in advance!!
This is bit tricky, because you only have the time, and you want it to wrap around to 0 after hitting 24 hours. My approach is to extract the number of seconds from START_DATE, add 5 minutes, then take the mod of this by 24 hours to wrap around to zero if it exceeds one day's worth of seconds.
UPDATE STUDENT
SET START_TIME = CAST(STR_TO_DATE(CAST(MOD((TIME_TO_SEC(START_TIME) + 300), 86400) AS CHAR(5)), '%s') AS TIME)
WHERE ID = 1
In the demo below, you can see the logic in action which correctly converts 23:55:00 with five minutes added to become 00:00:00.
SQLFiddle
However, the easiest solution in your case might be to just use a DATETIME and ignore the date component. Then the time should wrap automatically to a new day.
select addtime('23:55:00', '00:06:00');
output - 24:01:00 (Ideally it is right, because time datatype represents only time, if it converts to 00:01:00 then time component looses 24hr, which is wrong)
select addtime('2016-09-01 23:55:00', '00:06:00');
output - 2016-09-02 00:01:00 (In this case, 24hr gets added in date so time component is represented as 00:01:00)
If the requirement is to get it as 00:01:00 then here is the workaround -
SELECT TIME((ADDTIME(TIME('23:59:59'), TIME('02:00:00')))%(TIME('24:00:00')));
reference -
ADDTIME() return 24 hour time
In MS Access 2010, assume a query contains the following results:
Date Activity Hours
1.9. Reading 1
1.9. Writing 2
2.9. Reading 1
3.9. Talking 1
4.9. Reading 3
1.10. Talking 2
1.10. Writing 1
2.10. Reading 2
3.10. Talking 2
4.10. Reading 1
the Report should show the sum of hours spent each month grouped by activity, something like
Month Activity Hours
September Reading 5
Writing 2
Talking 1
October Reading 3
Writing 1
Talking 4
using the wizard I mange to get a report which looks like
Month Activity Hours
September Reading 1
Reading 1
Reading 3
Writing 2
Talking 1
October Reading 2
Reading 1
Writing 1
Talking 2
Talking 2
which is nearly what I'd like to have but.. ?
I tried =sum(Hours), but then all Hours fields simply contain the total sum of all hours, and I still get several lines for the same activity.
Actually the report is more complicated. In a form prior to the report, the user can enter a date range (e.g. September 1 - October 30). The report should sum over the whole time range and not break up to months, that is:
Date range Activity Hours
9/1 - 10/30 Reading 8
Writing 3
Talking 5
Create a query with a SQL like this:
SELECT MonthName(Month([Date])), Activity, SUM(Hours)
FROM yourTable
WHERE [Date] >= xxx AND [Date] <= yyy
GROUP BY MonthName(Month([Date])), Activity
The key element is the GROUP BY clause, it sums the Hours per Month + Activity.
Then base your report on that query.
BTW, it is not a good idea to call a column "Date", it's a reserved word in Access.
I want to create a query in Access that will sum up the total of time giving per project.
I have a database that look like:
Time | Project
------------------
1:00 | 1455
2:30 | 1666
0.50 | 1455
0.45 | 1455
I want to know how many hours were given to all project.
So far I have created a query that return the total of hours per project, which is good.
However, the format of the time is odd.For instance a project that should return 17:10 hours return this 0.715277777777778
I am trying to format the field Time so the total return a normal looking format like 17:10 hours instead of all those decimals.
This is the SQL I have right now
SELECT Sum(Dan.Time) AS SumOfTime, Dan.Project
FROM Dan
GROUP BY Dan.Project
ORDER BY Dan.Project;
Can anyone help?
You can use format function:
Format(Sum(Dan.Time),"hh:nn") AS SumOfTime
P.S. DateTime in ms Access are in fact double where 1 is one day, and 1/24 is one hour and so on.
I'm not working with ms access, but maybe this helps you.
All times should be saved in the same type. Maybe you should format the times before storing them in the database.
If it is not possible, you can try this
SELECT
Int(Sum(Dan.Time) * 24) & ":" & (Int((Sum(Dan.Time) * 24 - Int(Sum(Dan.Time) * 24)) * 60) + 1)
AS TimeStr,
Dan.Project
FROM
Dan
GROUP BY
Dan.Project
ORDER BY
Dan.Project;
It computs the hours and the minutes from the decimal number and concatenates the numbers as HH:MM.
Maybe there is also a better build in way in ms access.
SELECT *
FROM tablename
WHERE
MAKETIME(3,0,0) BETWEEN MAKETIME(23,0,0) AND MAKETIME(5,0,0)
is returning nothing And 3:00 is between 23:00 AND 5:00 time. Why is that can anyone explain me how to solve this problem?
It's unclear what you're actually trying to do here, because even if 3 were between 5 and 23 your query would simply return every record in the table.
SELECT MAKETIME(3,0,0) BETWEEN MAKETIME(5,0,0) AND MAKETIME(23,0,0)
Returns 0, because 3 is not between 5 and 23.
SELECT MAKETIME(5,0,0) BETWEEN MAKETIME(3,0,0) AND MAKETIME(23,0,0)
Returns 1, because 5 is between 3 and 23.
Demo: SQL Fiddle
Presumably you're trying to wrap into the previous day, in which case you can directly compare datetime values, but it's unclear given your question what fields/datatypes you're actually working with.
Update:
Based on your comment, I think you want 2 comparisons. 3 is not between 5 and 23, because time doesn't wrap across days. But if you only care about the time portion you can handle it like this:
SELECT *
FROM tablename
WHERE YourTime BETWEEN MAKETIME(23,0,0) AND MAKETIME(23,59,59)
OR YourTime BETWEEN MAKETIME(0,0,0) AND MAKETIME(5,0,0)
Remember that BETWEEN is inclusive, so if 5am is your cutoff time you may want it to be MAKETIME(4,59,59) so it includes 4:59 but not 5:00
Function MAKETIME returns a time value calculated from the hour, minute, and second arguments:
mysql> SELECT MAKETIME(3,0,0),MAKETIME(23,0,0),MAKETIME(5,0,0)
-> '03:00:00', '23:00:00', '05:00:00'
and, of course, 3 is not BETWEEN 23 AND 5 and it will return false. But yes, 3AM actually is between 11PM and 5AM, so how could you solve this?
Let's consider 23 as your START_TIME, and 5 as your END_TIME.
Since START_TIME has to happen before END_TIME, if this is not the case (23>5) that means that the interval rolls over the next day.
I would try with a query like this:
SELECT *
FROM yourtable
WHERE
(MAKETIME(START_TIME,0,0)<=MAKETIME(END_TIME,0,0) AND MAKETIME(3,0,0) BETWEEN MAKETIME(START_TIME,0,0) AND MAKETIME(END_TIME,0,0))
OR
(MAKETIME(START_TIME,0,0)>MAKETIME(END_TIME,0,0) AND NOT (MAKETIME(3,0,0) BETWEEN MAKETIME(START_TIME,0,0) AND MAKETIME(END_TIME,0,0)))