MySQL: Grouped results per record per day - mysql

I’ve got a table with two columns Time and Login, showing when a user has sent calls during a day.
On a per user basis, I need to find the greatest, smallest and average session length where the session length is calculated as the time in minutes between the first call and the last call on any given day.
Table:
Time Login
2012-05-29 20:01:26 A
2012-05-29 20:01:40 A
2012-05-29 20:02:27 A
2012-05-29 20:58:46 A
2012-05-29 20:59:50 A
2012-05-29 21:00:12 A
2012-05-29 21:00:36 A
2012-05-30 21:28:28 A
2012-05-30 21:29:08 A
2012-05-30 21:29:13 A
2012-05-30 21:29:25 A
2012-04-06 10:25:24 A
2012-04-06 10:25:53 A
2012-04-06 10:26:35 A
2012-04-27 12:05:45 A
2012-04-27 12:06:06 A
Desired output would look something like this:
MaxSession Login
59 A
As mentioned above the same output is required for MinSession and AverageSession.
I am completely stuck for hours and have really no clue how to get this done.
What I've achieved is to get the unique active days per Login.
I just cannot figure out how to achieve the next steps, though, which would be:
get the first entry per Login per day
get the last entry per Login per day
calculate the difference in minutes between last entry
and first entry per day per Login as SessionLength
select the Max, Min, and Average SessionLenght per Logins
I would be very grateful for any type of help!

Try this
Grouped by day
Select Login,Date(Time) As LoginDay,Max(Time) As LastLogin,Min(Time) As FirstLogin,Count(*) As Logins,
From LoginsTable
Group By Login,Date(Time)
Order By LoginDay,Login
Please enclose columns in ' ' for the columns like 'time'
Max,Min and average sessions
Select Login,Max(SessionMinutes) As MaxSessoin,Min(SessionMinutes) As MinSession,
Avg(SessionMinutes) From
(
Select Login,
LoginDay,LastLogin,FirstLogin,TotalLogins,Hour(DateDiff(LasTLogin-FirsTLogin))*60 + Minute(DateDiff(LasTLogin-FirsTLogin)) As SessionMinutes
From
(
Select Login,Date(Time) As LoginDay,Max(Time) As LastLogin,Min(Time) As FirstLogin,Count(*) As Logins,
From LoginsTable
Group By Login,Date(Time)
) As Temp
) As Temp1
Presently ignoring the seconds while calculating the minutes.

Related

Represent an event occurence on a time chart

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/

MySQL: Select count of returning vs. new rows in MySQL with period

I have database table in MySQL, which consist of the following fields:
id
user_id
timestamp
The table is a simple log of visitors. I am trying to get the following numbers in one query:
Distinct user_id's for a specific time period (30 days)
Amount of these user_id's, which already exist in the table, regardless of time period
I have been able to do it within the period with this simple query:
SELECT
COUNT(DISTINCT user_id) AS 'count_distinct',
COUNT(user_id) AS 'count_all'
FROM
table
WHERE
timestamp BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE();
Running this query gives me the count of distinct user_id's and the count of all user_id's within the time period. I can then apply the math myself to get the count of new vs. returning visitors - for that period. What I am trying to figure out is how many distinct user_id's, who visited within 30 days, who has also visited at any previous point in time.
I hope you can help me solve this.

How can I see how many times a person came per month (SQL)

For my stystem I want to know how many times a visitor came to my shop. I got wifi sensors and they get alot of addresses and I want to know how many times the visitors came in a month
This is the database I use (time is in unix time and get fixed with FROM_UnixTime(sensordata1.time)
So what I want to get is a the address with the number of visits last month.(per day not per address so if he came 5times a day count it as 1)
You want to see March 2017. So restrict your results to March 2017 in the WHERE clause. You want one result row per visitor (address). So GROUP BY address. You want to count each day just once. So COUNT DISTINCT days.
select
address,
count(distinct from_unixtime(sensordata1.time, '%Y-%m-%d'))
from sensordata1
where from_unixtime(sensordata1.time, '%Y-%m') = '2017-03'
group by address;
If you want this more flexible, i.e. always the last month when executing the query instead of 2017-03 fixed, then find today, subtract a month, and take the month got thus.

convert to hours and sum the hours worked mysql

I have a column name duration with time in seconds for example i have 28000 seconds now i want to convert the second to hours and sum total number of hours for a users i have list of users with hours works which is stored in seconds like 28000 here my query
SELECT ohrm_project.project_id AS ohrmprojectid, ohrm_project.customer_id,
ohrm_project.name AS ohrm_projectname,
ohrm_timesheet_item.duration AS duration,
ohrm_timesheet_item.date AS date,
TIME_FORMAT(SEC_TO_TIME(sum(ohrm_timesheet_item.duration),"%Hh %im")) as totalhoursworked,
hs_hr_employee.*
FROM ohrm_project,ohrm_timesheet_item,hs_hr_employee
WHERE ohrm_timesheet_item.employee_id=hs_hr_employee.employee_id AND
ohrm_timesheet_item.project_id=ohrm_project.project_id AND
ohrm_project.project_id=2 AND
ohrm_timesheet_item.date BETWEEN '2016-03-31' AND '2016-04-06'
I tried query but i am getting total hours worked but i am not getting in correct i want to get suppose i have two user i want to get total hours work for 1st person and total hours for second user but i am getting only 1 user with total hours of users like this i am passing my screenshot see

SQL average number of requests per user over time period

I have a table (Requests) with the following fields:
id, requestType, userEmail, date
I want to find the average number of requests per user over a given period (i.e. over the last month). Any suggestions?
Thanks!
Greg
Something like SUM feature will work. Might be a little slow.
SELECT SUM(requestType) FROM Requests WHERE `userEmail` = `userEmail` and `date` BETWEEN `first-date YYYY-MM-DD` AND `second-date YYYY-MM-DD`;
SQL SUM
I would also recommend, if you have a lot of request, to have one row per user per day and just update the request total for that user.
Edit: If you want the last 30 days something like this query should work. It worked on my test table.
SELECT SUM(requestType) FROM Requests WHERE `userEmail` = `userEmail` and `date`BETWEEN curdate() - INTERVAL 30 DAY AND curdate();