I'm calculating the sum of two columns from the same table with SUM() but the end result is an integer (286676). I'm guessing it's milliseconds? How can I convert to TIME(00:00:00)?
database
id|hours_worked | hours_worked_wk2 |
hours_worked = 14:33:38
hours_worked_wk2 = 14:33:38
Query
SELECT *,SEC_TO_TIME(SUM(TIME_TO_SEC(ep.hours_worked)))+SEC_TO_TIME(SUM(TIME_TO_SEC(ep.hours_worked_wk2)))
AS TotalHoursWorked
FROM employeepayroll ep
JOIN employees em ON ep.employee_id=em.employee_id
JOIN payroll p ON ep.payroll_id=p.payroll_id
JOIN payrolltaxes pt ON ep.payroll_id=pt.payroll_id
WHERE ep.timesheet_status='Approved' AND p.pay_group='26'
ORDER BY ep.payroll_id DESC
TotalHoursWorkd = 286676
Use the following formula.
hours = cast(duration_in_milliseconds \ (60 * 60 * 1000) as int)
mins = (duration_in_milliseconds \ (60 * 1000)) mod 60
secs = (duration_in_milliseconds \ 1000) mod 60
Your query will look something like this:
select cast(duration_in_milliseconds\(60*60*1000) as int)+':'((duration_in_milliseconds \ (60*1000)) mod 60;)+':'((duration_in_milliseconds \ 1000) mod 60) from something
When you see it...
14:33:38 >>> 143,338 × 2 = 286,676
Yikes. The numbers in the times are being implicitly cast to integers.
You are adding the two values of SEC_TO_TIME(SUM(...))+SEC_TO_TIME(SUM(...)).
Instead, use SEC_TO_TIME(SUM(...) + SUM(...)).
Related
I have this javascript code that works fine:
function timeup(s) {
var d, h, m, s;
m = Math.floor(s / 60);
s = s % 60;
h = Math.floor(m / 60);
m = m % 60;
d = Math.floor(h / 24);
h = h % 24;
m = m > 9 ? m : "0"+m;
h = h > 9 ? h : "0"+h;
s = s > 9 ? s : "0"+s;
if (d > 0) {
d = d+" days ";
} else {
d = "";
}
return d+h+":"+m+":"+s;
}
SO i need same function but in MySQL(because i do SQL query and don't want to use javascript conversion on client side)
So i need to convert in MySQL seconds to get this same output:
timeup(600000) => 6 days 22:40:00
timeup(60000) => 16:40:00
timeup(6000) => 01:40:00
timeup(600) => 00:10:00
timeup(60) => 00:01:00
timeup(60) => 00:01:00
timeup(6) => 00:00:06
So if seconds below day show HH:MM:SS if seconds greater that day show X days HH:MM:SS
I im trying using CONCAT & TIMESTAMPDIFF but i think maybe it should go if then to compare day below 24h or grater to show custom string X days...any help welcome.
I tested this and it seems to do the job:
DROP FUNCTION IF EXISTS GET_HOUR_MINUTES;
DELIMITER $$
CREATE FUNCTION GET_HOUR_MINUTES(seconds INT)
RETURNS VARCHAR(16)
BEGIN
RETURN CONCAT(LPAD(FLOOR(HOUR(SEC_TO_TIME(seconds)) / 24), 2, 0), ' days ',TIME_FORMAT(SEC_TO_TIME(seconds % (24 * 3600)), '%H:%i:%s'));
END;
$$
DELIMITER ;
Test it like this:
SELECT GET_HOUR_MINUTES(600001);
That returns
'06 days 22:40:01'
It seems to want, at least in MySQL Workbench, to have the database you are using selected before you run it. It saves the function within the database, that is, you can see it in the column on the left with Tables, Views, Stored Procedures and Functions.
I now have another problem with this above function that works only on seconds..but i forget to ask in first question that i have in database stored number:
uptime => 1507977507423
And i need to get seconds and show above format from NOW() time
So for example if i have uptime in database so formula will be: NOW() - uptime, i try using this but i get strange output like 34 days 838:59:59 and that is not correct:
SELECT
CONCAT(LPAD(FLOOR(HOUR(SEC_TO_TIME(UNIX_TIMESTAMP(NOW())-SUBSTRING(uptime, 1, length(uptime) - 2))) / 24), 2, 0), ' days ',TIME_FORMAT(SEC_TO_TIME(UNIX_TIMESTAMP(NOW())-SUBSTRING(uptime, 1, length(uptime) - 2) % (24 * 3600)), '%H:%i:%s')) AS nice_date
FROM streams
WHERE id=1;
I get this:
+-------------------+
| nice_date |
+-------------------+
| 34 days 838:59:59 |
+-------------------+
I am trying to multiply a time with decimal value but not to get it right. That is, when I multiply with any decimal value, it is rounding that value to nearest integer and then doing the multiplication.
For Example: All the below operations are resulting the same value
1. '00:04:18' * 1.7 = '00:08:36'
2. '00:04:18' * 1.8 = '00:08:36'
3. '00:04:18' * 1.9 = '00:08:36'
4. '00:04:18' * 2 = '00:08:36'
Please let me know what change I have to do to get the accurate value without rounding.
Thanks
SEC_TO_TIME( TIME_TO_SEC( '00:04:18' ) * 1.7 ) = '00:07:18.6'
SEC_TO_TIME( TIME_TO_SEC( '00:04:18' ) * 1.7 ) = '00:07:44.4'
I have a MySQL table which looks like this:
id load_transit load_standby hours_transit hours_standby
1 40 20 8 4
2 30 15 10 10
3 50 10 3 9
I need to do the following calculations:
(intermediate calculations)
hours_transit_total = 8+10+3 = 21
hours_standby_total = 4+10+9 = 23
(desired result)
load_transit_weighted_mean = 40*(8/21) + 30*(10/21) + 50*(3/21) = 36.667
load_standby_weighted_mean = 20*(4/23) + 15*(10/23) + 10*(9/23) = 13.913
Is it possible to do this in a single query? What would the best design be?
Note that
40*(8/21) + 30*(10/21) + 50*(3/21) =
(40*8)/21 + (30*10)/21 + (50*3)/21 =
(40*8 + 30*10 + 50*3)/21
and
20*(4/23) + 15*(10/23) + 10*(9/23) =
(20*4)/23 + (15*10)/23 + (10*9)/23 =
(20*4 + 15*10 + 10*9)/23
Which allows you to get the results you want using
SELECT sum(hours_transit * load_transit) / sum(hours_transit),
sum(hours_standby * load_standby) / sum(hours_standby)
FROM your_table
I just had this same question and built this little query I think makes it clear how to find the weighted average in a single query:
select sum(balance), sum(rate * balance / 5200) as weighted_rate, -- what I want
-- what you cannot do: sum(rate * balance / sum(balance))
sum(balance * rate) / sum(balance) as weighted_rate_legit -- ah thank you transitive math properties
from (
select '4600' as balance, '2.05' as rate from dual
union all
select '600' as balance, '2.30' as rate from dual
) an_alias;
Can I ask for help on a SQL Statement please, I have to do the calculation inline and cannot declare variables for it
Calculation:
-91000000 / 2700000 = -33.7037037037
I need the remainder (7037037037 - but only up to 6 characters ) to be multiplied by 15000
703703 / 15000 = Final Answer of 49.913533
I thought I could do this:
select cast(ParseName(abs(cast(-91000000 as decimal)/ 2700000 ) %1,1) as numeric(8,8)) / 15000
WITH cte AS
(
SELECT -91000000 AS x, 2700000 AS y
)
SELECT ABS(ROUND((CAST(x AS decimal) / CAST(y AS decimal)) - (x/y), 6)) * 1000000 / 15000 FROM CTE
I have a field on my table which represents seconds, I want to convert to minutes
Select (100/60) as Minute from MyTable
-> 1.66
How can I get 1 minute and 40 seconds 00:01:40 and then round to 00:02:00 and if 00:01:23 round to 00:01:30
Using Mysql.
There are two ways of rounding, using integer arithmetic and avoiding floating points, a value to the nearest thirty seconds...
((seconds + 15) DIV 30) * 30
(seconds + 15) - (seconds + 15) % 30
The latter is longer, but in terms of cpu time should be faster.
You can then use SEC_TO_TIME(seconds) to get the format hh:mm:ss, and take the right 5 characters if you really need hh:mm.
If you wanted to avoid SEC_TO_TIME(seconds), you can build up the string yourself.
minutes = total_seconds DIV 60
seconds = total_seconds % 60
final string = LPAD(minutes, 2, '0') | ':' | LPAD(seconds, 2, '0')
i am not sure about how to round it but you can convert seconds into time i.e hh:mm:ss format using SEC_TO_TIME(totaltime)
Desired result :
A = 30
B = 60
C = 90
D = 120
select
(25 + 15)-(25 + 15) % 30 as A,
(32 + 15)-(32 + 15) % 30 as B,
(90 + 15)-(90 + 15) % 30 as C,
(100 + 15)-(100 + 15) % 30 as D
Result :
A = 30
B = 30
C = 90
D = 90
I try with this:
select
30* ceil(30/30) as A,
30* ceil(32/30) as B,
30* ceil(90/30) as C,
30* ceil(100/30) as D
Result :
A = 30
B = 60
C = 90
D = 120
Thank you for your help !
You can simply write your own function http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html
But I'd rather do that in a programing language (PHP, Python, C), not on the database side.