I have an input form where the user will input their start time and their start time. I then have a function that calculates the time difference and stores the hours and minutes worked in a new field.
What I want to do is calculate the total time as one value in Hours and Minutes.
The following SQL will bring all the meta_value stored in the database
SELECT wp_frm_item_metas.meta_value
FROM wp_frm_item_metas
WHERE 1=1
AND wp_frm_item_metas.field_id = '103'
This returns values as follows (first 5 only)
meta_value
2:15
1:15
2:15
4:15
0.00
If I use the following
SELECT SUM(wp_frm_item_metas.meta_value) AS total_hours
FROM wp_frm_item_metas
WHERE 1=1
AND wp_frm_item_metas.field_id = '103'
I do not get the total in hours and minutes so I wonder if anyone could help me to achieve this please?
Thanks
Wayne
Use TIME_TO_SEC to convert to seconds, then SUM. For output you could use SEC_TO_TIME function to get hours:mins. E.g:
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(wp_frm_item_metas.meta_value))) AS total_hours
FROM wp_frm_item_metas
WHERE 1=1
AND wp_frm_item_metas.field_id = '103'
I haven't tested the above but should get you close...
Related
i am facing an issue with my query i store users login time_in and time_out and a column name called break where i manually enter how long user has been on break supposing an hour then 01:00:00 i store it on the break column and to get total login hours of a user i use
TIME_TO_SEC(TIMEDIFF(time_out, time_in)) AS totalhours
now from the above query how can i subtract the value i store it on break column and then show the total hours? only if break column has some value then subtract else show the correct total hours.
e.g if say users total login hours is 9hrs but then in break i added 1hr then the totalhours instead of showing 9hrs should rather display 8hrs.
here is my complete query
SELECT hours_id,
member_id,
username,
team,
time_in,
time_out,
break,
activity,
comments,
TIME_TO_SEC(TIMEDIFF(time_out, time_ink)) AS totalhours
FROM login_hours
WHERE DATE_FORMAT(date, '%Y-%m-%d') LIKE '".$filter_date."'
ORDER BY activity DESC
Really appreciate your help.
thanks
I sorted it by
TIME_TO_SEC(TIMEDIFF(time_out, time_in)) - TIME_TO_SEC(break) AS totalhours
it does gives the result im looking for. can someone tell me if its right?
I am using the Graph Reports for the select below. The MySQL database only has the active records in the database, so if no records are in the database from X hours till Y hours that select does not return anything. So in my case, I need that select return Paypal zero values as well even the no activity was in the database. And I do not understand how to use the UNION function or re-create select in order to get the zero values if nothing was recorded in the database in time interval. Could you please help?
select STR_TO_DATE ( DATE_FORMAT(`acctstarttime`,'%y-%m-%d %H'),'%y-%m-%d %H')
as '#date', count(*) as `Active Paid Accounts`
from radacct_history where `paymentmethod` = 'PayPal'
group by DATE_FORMAT(`#date`,'%y-%m-%d %H')
When I run the select the output is:
Current Output
But I need if there are no values between 2016-07-27 07:00:00 and 2016-07-28 11:00:00, then in every hour it should show zero active accounts Like that:
Needed output with no values every hour
I have created such select below , but it not put to every hour the zero value like i need. showing the big gap between the 12 Sep and 13 Sep anyway, but there should be the zero values every hour
(select STR_TO_DATE ( DATE_FORMAT(acctstarttime,'%y-%m-%d %H'),'%y-%m-%d %H')
as '#date', count(paymentmethod) as Active Paid Accounts
from radacct_history where paymentmethod <> 'PayPal'
group by DATE_FORMAT(#date,'%y-%m-%d %H'))
union ALL
(select STR_TO_DATE ( DATE_FORMAT(acctstarttime,'%y-%m-%d %H'),'%y-%m-%d %H')
as '#date', 0 as Active Paid Accounts
from radacct_history where paymentmethod <> 'PayPal'
group by DATE_FORMAT(#date,'%y-%m-%d %H')) ;
I guess, you want to return 0 if there is no matching rows in MySQL. Here is an example:
(SELECT Col1,Col2,Col3 FROM ExampleTable WHERE ID='1234')
UNION (SELECT 'Def Val' AS Col1,'none' AS Col2,'' AS Col3) LIMIT 1;
Updated the post: You are trying to retrieve data that aren't present in the table, I guess in reference to the output provided. So in this case, you have to maintain a date table to show the date that aren't in the table. Please refer to this and it's little bit tricky - SQL query that returns all dates not used in a table
You need an artificial table with all necessary time intervals. E.g. if you need daily data create a table and add all day dates e.g. start from 1970 till 2100.
Then you can use the table and LEFT JOIN your radacct_history. So for each desired interval you will have group item (group by should be based on the intervals table.
I am having trouble understanding the structure of the query i wish to perform. What i have is a large set of data in a table with multiple UnitID's. The units have temperatures and Timestamps of when the temperatures where recorded.
I want to be able to display the data where I can see the Average temperature of each unit separated in a weekly interval.
Apologies for my previous post, I'm still a novice with querying. But i will show you what i have done so far.
SELECT UnitID AS 'Truck ID',
AVG(Temp) As 'AVG Temp',
LogTime AS 'Event Time',
DAY(g.`LogTime`) as 'Day',
MONTH(g.`LogTime`) as 'Month',
COUNT(*) AS 'Count'
FROM `temperature` as g
WHERE DATE_SUB(g.`LogTime`,INTERVAL 1 WEEK)
AND Ana > 13 AND Ana < 16 AND NOT g.Temp = -100
GROUP BY 'truck id', YEAR(g.`LogTime`),MONTH(g.`LogTime`),WEEK(g.`LogTime`)
Order BY 'truck id', YEAR(g.`LogTime`),MONTH(g.`LogTime`),WEEK(g.`LogTime`)
;
(Sorry, I don't know how to display a table result at the moment)
This result gives me the weekly temperature averages of a truck, and shows me on which day of the month the temperature was recorded, as well as a count of temperatures per week, per truck.
The Query I want to perform , creates 5 columns, being UnitID, Week1, Week2, Week3, Week4.
Within the 'Week' columns I want to be able to display a weekly(Every day of the Week) temperature average for each truck, where the following week is set a week after the previous week (ie. Week2 is set to display the avg(temp) one week from Week1).
And this is where I am stuck on the structure of how to create the query. Im not sure if i need to create sub-queries or use a Union clause. I have tried a couple of queries , but i have deleted them because they did not work. I'm not sure if this query is too complex or if its even possible.
If anyone will be able to help I would greatly appreciate it. If there is any other info I can supply that will help, I will try to do so.
Hopefully this is solvable. :p
MySQL has a WEEK function that will return the week of the year as an integer (0-52). You can use that in you GROUP BY clause, and then use the AVG aggregation function to get the average temperature. Your query would look something like this:
SELECT unitID, WEEK(dateColumn) AS week, AVG(tempColumn) AS averageTemperature
FROM myTable
GROUP BY unitID, WEEK(dateColumn);
Here is a list of other helpful Date and Time Functions that may be useful for querying your database.
I am looking to pull scheduled hours in a given time period. Our start and end schedule times are datetimes so I converted them to timestamps. When I dont sum them everything looks correct, but when I sum them over a time period, the output isnt in a timestamp format and the numbers are incorrect.
The query I am using:
select sal.public_name, sum(timediff(timestamp(end_time), timestamp(start_time)))
from bi.support_agents_list sal
join bi.support_sp_shifts_scheduled ss
on ss.agent_sp_id = sal.sp_id
join bi.support_sp_shifts s
on s.pk_id = ss.pk_id
where date(start_time) between '2014-01-29' and '2014-01-31'
group by sal.public_name
A few examples of results I am getting:
Agent 1: 53000 - when it should be 5.5 hours or 5:30
agent 2: 196000 - when it should be 20 hours
Any thoughts on this? I would prefer my output to be in an hour count so 5 hours and 30 min is formatted as 5.5 rather than 5:30.
try this instead of the sum
date_format(timediff(timestamp(end_time), timestamp(start_time)),
'%k hours, %i minutes, %s seconds') as thesum
like that
select sal.public_name,
date_format(timediff(timestamp(end_time), timestamp(start_time)), '%k hours, %i minutes, %s seconds') as thesum
from bi.support_agents_list sal
When doing aggregate calculations with datetime sum(datetime), the result is not what you expect (=cannot sum datetimes). You will be better off converting the datetime to seconds before the aggregate function and then convert it back to time.
Your aggregate function call would then look something like:
select sec_to_time(sum(unix_timestamp(end_time)-unix_timestamp(start_time)))
Be aware that you may reach maximum value that time datatype can contain and that unix_timestamp starts from 1970.
my query :
SELECT (
`total_hours`
)
FROM work_details
WHERE `employee_id` = '28'
AND DATE
BETWEEN '2012-02-01'
AND '2012-02-01'
LIMIT 0 , 30
// Which takes total hours worked for a given employee in given date from database table work_details
result:
total_hours
02:27:29
00:13:56
03:03:49
00:00:03
00:30:20
01:04:13
//result shows the times an employee worked in a given date,I need to get the total of these values to find total working hour in a given date.
how to get sum of these values in a field???
if i use sum(total_hours) result would be
sum( total_hours )
67870
and that is not correct.I require it in time type itself.
And i have one more help needed, this query worked because i gave same date for the BETWEEN clause.
DATE
BETWEEN '2012-02-01'
AND '2012-02-01'
But i need to calculate total working hours of days in a given range , say
DATE
BETWEEN '2012-02-01'
AND '2012-02-29'
Since a given date has multiple total hours entry , i need get total hours for each distinct day.Help??
try this (taken from here):
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(`total_hours`))) ... -- rest of your query