SELECT SUM(render) AS total FROM mytable
This is my code for getting the summation of all rendered hrs. But the output turned out to be like for instance I have 04:30:00 and 03:15:00, then the output turned to 074520, but I want it to be like 7.75 hrs
Here is one way to do it in MySQL:
SELECT
SUM(HOUR(render) * 60.0 + MINUTE(render)) / 60.0 AS TotalHours
FROM mytable
The expression HOUR(render) * 60.0 + MINUTE(render) converts time value into the number of minutes which we sum up to get the total.
You can add SECOND() to the formula to get a more precise answer if you want.
If you want to sum a time type in MySQL, use:
select sum(time_to_sec(render))
from mytable;
This converts the time to a seconds value, sums the value, Then, if you want to convert it back to another format, convert the seconds:
select sum(time_to_sec(render)) / (60 * 60) as decimal_hours,
sec_to_time(sum(time_to_sec(render))) as time_format
MySQL does not directly sum time values. It is going to convert them to a number of the form HHMMSS. So, 00:00:55 is 55. If you add it to itself you get 110, which has nothing to do with 00:01:50.
Related
The type of data in column "length" is varchar.
I need to calculate average length of audio.
Do I need to change data type to datetime or is there a way not to do this?
I am afraid, if I do it , my values may become incorrect.
Thanks!
Example
Length
22:32:00
11:22:12
10:00:00
You should change the data type to time because in its current format you cannot do any time calculations on your data without converting it to time anyway.
As mysql manual on the time data type says:
TIME type can be used not only to represent a time of day (which must be less than 24 hours), but also elapsed time or a time interval between two events (which may be much greater than 24 hours, or even negative).
If you are worried about loosing some data due to incorrect formatting, then test varchar to time conversion using str_to_date() function. Wherever it returns null with the proper mask used, your data is in the wrong format.
Switch your data format to a number -- but the number of seconds of duration. You won't be limited to arbitrary limits on time and you can readily use the value for calculations.
If your length value is always 8 characters, you can do:
select (substring_index(length, ':', 1) * 60 * 60 +
substr(length, 4, 2) * 60 +
substring_index(length, ':', -1)
) as duration_seconds
If, after calculations, you want to represent the value as HH:MM:SS, you can convert back to that format.
The answer is no. It is like can you multiply YourNAme and HisName?
So it should be converted to a real number.
However, you can still do some addtion or multiplication using mysql syntax alone. The engine will implicitly convert it for you.
From your example, every row has hh:mm:ss, right? where : is the column separation?
If not yet separated, use substring or regex to separate it.
Assuming the hh, mm, and ss are separated.
You can actually perform arithmetic using SQL
SELECT
(SECONDS + MINUTES*60 + HOURS*60*60 ) as 'LengthInSeconds'
FROM yourTable
If you didn't want to separate to columns, just use substring or regex to separate during query.
You can now just sum all the queries and divide it by Ntimes
After you get the value. You convert it back to hours, minutes, and seconds. See sample code below.
//
$average = $totalAccumulatedSeconds/3;
// Time calculations for hours, minutes and seconds
$hours = Math.floor(($average % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
$minutes = Math.floor(($average % (1000 * 60 * 60)) / (1000 * 60));
$seconds = Math.floor(($average % (1000 * 60)) / 1000);
Actually you can perform several tests, if you really are afraid of calculations :)
I have the time value in one column like below.
Now I need to sum this column and convert the result to minutes.
You can convert the time to seconds and then divide by 60 to get minutes. Something like:
SELECT SUM(TIME_TO_SEC(`activeTime`))/60 FROM tableName
That will give you the decimal version of the minutes (e.g. 5.8 minutes = 5 minutes and 48 seconds). If you want the minutes in time notation (e.g. 05:48):
SELECT SEC_TO_TIME( SUM(TIME_TO_SEC(`activeTime`)) ) FROM tableName
This can help you:
Convert time into seconds,in your case column name is activeTime:
SELECT TIME_TO_SEC('00:12:0');
SELECT TIME_TO_SEC(your_column);
Perform sum operation:
SELECT SUM(output_of_TIME_TO_SEC) AS "total second" FROM table_name;
I created a field called 'hours_spent' in MySQL using the decimal datatype to store time. The values are stored like this 1.30, 2.30 etc... (for 1hr30min, 2hr30min).
I want to calculate the sum of various time values.
The sum of time is not what I expected: 1.30 + 2.30 = 3.60, whereas I expected 4.00.
I used the SUM function in MySQL to count the hours_spent field. If the values are 0.30 + 1.50 = 1.80, whereas I expected 2.20.
My first mistake was to use the decimal type instead of the time datatype, but I cannot change datatype.
So, is there any way to sum the time values and get result as I expect?
Thanks
I prepared you a demo at sqlfiddle, you can try it there if you want:
http://www.sqlfiddle.com/#!2/c9afc/2
Here are the query samples:
select #indexer:=instr(dateasdecimal, '.')
, left(dateasdecimal, #indexer-1) * 60 + substr(dateasdecimal, #indexer+1) as totalMinutes
from testtable;
select #indexer:=instr(dateasdecimal, '.')
, sum(left(dateasdecimal, #indexer-1) * 60 + substr(dateasdecimal, #indexer+1)) as totalMinutes
from testtable;
Note: Please don't forget to accept answers to your questions:
https://meta.stackexchange.com/a/65088/200585
To convert a decimal into seconds, you could use this:
truncate(hours_spent,0)*60+(hours_spent-truncate(hours_spent,0))*100
and then you can do the sums easily. Then you can convert back seconds to the decimal format with this:
truncate(seconds/60,0)+truncate(mod(seconds, 60)/100,2)
You could always turn the decimals into a string, cast as time, then sum that time using time_to_sec and produce a formatted time with sec_to_time. Of course, it would be much better to be storing those times a different way, even if it involves converting the entire dataset.
SELECT sec_to_time(sum(time_to_sec(goodTime))) FROM (
SELECT CAST(badTime AS TIME) AS goodTime FROM (
SELECT REPLACE(badTime, '.', ':') AS badTime FROM (
SELECT CAST(badTime AS dec(4,2)) AS badTime FROM (
SELECT 1.3 AS badTime
UNION select 2.3
) z
) y
) x
) w
I need to calculate time datatype in sql for the purpose of calculate employee's monthly working hour which obviously will grater than 24 hours(it should be at least 132hours).But the time data type doesn't supports greater than 23:59:59.9999999 . But i need to calculate monthly working hour.
Are you sure Total working time is given as Time datatype?
Your sample input is '32:23:00' which is not supported by 'time' Data type
Because its range is 00:00:00.0000000 through 23:59:59.9999999.
If it is Time datatype, then you can calculate the salary as juergen states:
select cast(cast (100.00 as money) * datediff(second, 0, '23:23:00') / (60.0 * 60.0) as money)
I cast the values into money data type to show that, its possible to multiply it with datediff() functions returned value.
You can substact dates with the DATEDIFF function.
The result can be mutiplied with your salary.
SELECT DATEDIFF(minute, '12:00:00', '12:30:00') / 60.0 * 100.0
I have tried with this code and the problem is solved now.
sum(datediff(second, 0, timeYouWantToSum)) / 3600.0
will give you the number of worked hours as a decimal.Now I am able modify this total working hour by round/floor/ceiling and also able to multiply with salary per hour.Thank you
I have complicated query over very big table.
Long story short, when I use convert time to select period of day (let's say 12-13h, converting it from datetime row) query takes few minutes, instead of few seconds without convert!
So, I tried datepart, and it works well, almost instant, but, problem is, how to point to hours and minutes in same time?
Any other fast solution is more than welcome.
Thanks.
Meanwhile I came up with this:
DATEPART(HOUR, datetimecolumn)*100 + DATEPART(MINUTE, datetimecolumn)) between 1210 and 1540
You can use datePart if you are willing to do a bit of math, as shown below:
12:10 = 12 * 60 + 10 = 730 minutes
15:40 = 15 * 60 + 40 = 940 minutes
select * .....
where datepart(mi, datefield) between (12*60+10) and (15*60+40)
If you have a constant periods - i.e. - always hourly and no any floating periods - you may introduce something like "ordinal number of period" calculated field, index on it and query of it with precalculated period value
OR
is there are no any constant periods - try to calculate proper begin and end values prior to SELECT statement and use them in the query.
Keep in mind that using functions in where clause of query - sometimes is a bad idea. Using functions in ORDER BY clause - always bad
You can get GETTIME from following Function
alter function GetTimeOnly(#_DateTime DateTime)
returns datetime
as
begin
return dateadd(day, -datediff(day, 0, #_datetime), #_datetime)
end
go
OR YOU CAN HAVE THE TIME FROM CONVERT FUNCTION.
SELECT
CONVERT(VARCHAR(8),GETDATE(),108) AS HourMinuteSecond,
CONVERT(VARCHAR(8),GETDATE(),101) AS DateOnly