convert to hours and sum the hours worked mysql - 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

Related

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 to get Weekly data from MySQL database table using week number of month?

I have a database table studentvideos. I want to retrieve data from this table based on week number of month which means for how much time a video is watched in a week and time is specified as totaltime column in table structure and noofviews column specifies how many times a video is watched. Week number is specified as a column in the table structure as wofmonth.
Lets we have a video named vid1, vid1 is watched two time each for 10 secs in week 1 and one time for 10 secs in week 2.
I want to get back response in nested array/JSON form in which I have week number as tag for each week and for each week number video name, sum of the time for which that video is watched in that specific week (E.g. week 1 array will have vid1 with time 20 secs and week 2 array will have vid1 with time 10 secs) and similarly number of time that video is watched in that week.
Screenshot of studentvideos table:
I tried the following query but I don't know how to modify or rewrite the query to get the desired result explained above.
SELECT videoid, sum(noofviews), sum(totaltime) FROM studentvideos group by videoid
Result:
Result of above query:
After trying so many different things the answer to this was simple. I am posting correct answer, may be someone face such a problem in future. Multiple GROUP BY did the trick for me.
Query:
SELECT wofmonth as weekNumber,videoid,sum(noofviews)as totalTime,sum(totaltime) as noOfViews from studentvideos group by wofmonth,videoid

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.

Getting JSON data in Splunk based on timestamp

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.

MySQL: Grouped results per record per day

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.