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/
Related
Here is a sample table that I am using,
User_id timestamp action
1 2020-10-01 09:00:00 Opened page
1 2020-10-01 09:10:00 Closed page
2 2020-10-02 04:00:00 Signed up
3 2020-10-02 06:00:00 Opened page
3 2020-10-03 11:00:00 Made a booking
3 2020-10-03 09:30:00 Closed page
need to write a SQL query to find the average time spent by a user on the page.
The expected answer is just a number which represents the average time spent by an average user on the page.
You can’t use SQL to calculate how much time a user spends on different pages of your UI application. You will need to implement this logic on your UI whenever there is an event such as when the user navigates to another page or a button click etc. You capture the timestamps you need on the UI and then make a database call through an SP call or Query through your server side code (such as .Net, Java or Node.js).
Once you have captured the data from the UI you will be able to implement any kind of logic on that data through an SP or a function or something like that in using SQL.
If you use TIMESTAMPDIFF(), and set its argument to SECOND, you can get back the difference of two datetime fields in a record in a manner that can be summed and divided. Documentation:
Returns datetime_expr2 − datetime_expr1, where datetime_expr1 and datetime_expr2 are date or datetime expressions.
Then use SUM() to sum up these values, and divide by the results of COUNT(). Documentation:
SUM(): Returns the sum of expr. If the return set has no rows, SUM() returns NULL.
COUNT(): Returns a count of the number of non-NULL values of expr in the rows retrieved by a SELECT statement.
Your code will then basically look like this. You may need to make some adjustments based on your database setup.
SELECT
SUM(
TIMESTAMPDIFF(SECOND, OrigDateTime, LastDateTime)
) / (select COUNT(id) FROM yourTable)
AS average
FROM yourTable;
This, of course, follows our standard formula for calculating an average:
sum(differences) / count(differences)
I have been thinking about this all afternoon and not sure if this is possible or not through only a query. test has two timestamp columns (start_time and end_time). I want to group by hour and add the seconds within that hour to a total.
So for instance, given a single row in test with these column values :
start_time = 1559865600 (06/07/2019 # 12:00:00am (UTC))
end_time = 1559876710 (06/07/2019 # 3:05:10am (UTC))
Result would be :
0 = 3600
1 = 3600
2 = 3600
3 = 310
4 = 0
...
23 = 0
There would be lots of rows of course. The goal is to create results which show 'how active' each hour of the day is.
Editing this as someone voted to close it. Think of the start and end timestamps as when an employee clocks in and clocks out. You have x amount of employees. The results would show the total amount of seconds all employees are clocked in for each hour of the day. You could then see what hours of the day are the 'most active' hours in terms of employees working. Hopefully that is clearer for some of you having a hard time understanding what I am looking to do. That is the best example I could come up to get across what I am looking to do.
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
I have some incident data in splunk which includes a lot of fields number, created_on etc. Now i query data based on time. There are five cases.
Last 60 minutes
Last 24 hours
Last 30 days
Last 5 weeks
Last 5 months
So every number is created on some specific time which is in created_time.
Now based on the time that i query, what i want is a JSON based on the timestamp.
For example,
If I select 60 minutes, i want incidents that were created in every minute.
At 4:01:00, i want the list of incident numbers.
Then the next list at 4:02:00.
Similarly for last 24 hours,
Between 4:00:00 and 5:00:00 , the list of incident numbers.
And from 5:00:00 and 6:00:00 and so on till next day 4:00:00-5:00:00
Example JSON
{"141000213" : {"IN00102", "IIN9239", "IN3u293"},
"141000234" : {"IN87208"},
"141000225" : {"IN03002", "IIN9239"}
}
Really new to Splunk. If anyone could help me out in this, it would be really helpful.
I'm in need of a method to store a time duration in a db field. I'm building a website where customers should be able to choose how long they would like an advert to display from a particular start date.
I had thought about using TIME but that has a max of '838:59:59' which works out at about 34 days. Its possible that a client would want an advert to exist for longer than that.
So what would be the best way to deal with this? Just a really large INT?
If you intend to have a column for start time and one for duration, I think you can store it in seconds. So, I assume you will have something like this;
+-----------+--------------------------+------------------+
| advert_id | start_time | duration_seconds |
+-----------+--------------------------+------------------+
| 2342342 |'2012-11-12 10:23:03' | 86400 |
+-----------+--------------------------+------------------+
(For the sake of the example, we will call this table adverts)
advert_id - a key pointing to your advert
start_time - the time the advert should start (data type - TIMESTAMP)
duration_seconds - Time in seconds that the advert is supposed to "live" (INTEGER(11)
SELECT TIME_TO_SEC(timediff(now(),start_time)) as 'time_difference_in_seconds_since_advert_started' FROM adverts;
If you want to get only adverts that have not expired, you will run a query like this;
SELECT * FROM `adverts` WHERE TIME_TO_SEC(timediff(now(),start_time))<=`duration_seconds`;
That's one way I would do it if I were to go with the "duration" field.
Yes, you can store time as INT data type (or another big integer: MEDIUMINT, LONGINT). Then use you can easily get days and time part from this, e.g. -
SELECT time DIV 86400 AS days, SEC_TO_TIME(column1 MOD 86400) AS time FROM table
Where 86400 is a number of seconds in 24h (60 * 60 * 24 = 86400).
not the best solution but you can add one column in your db, and check when time is more than 24 hours, calculate it as 1 day and write in that column, and all the rest time write in time column. But selecting from db you should calculate also that column of days