2

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??

3
  • no , it not working, 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. Thank You :) Jun 20, 2012 at 13:46
  • Is total_hours a varchar field? Jun 20, 2012 at 13:49
  • More info about why SUM(time) does not work in MySQL here: bugs.mysql.com/bug.php?id=12108
    – poncha
    Jun 20, 2012 at 13:58

1 Answer 1

4

try this (taken from here):

SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(`total_hours`))) ... -- rest of your query
4
  • haha :) Its working :) Thanks Again :) I have one more doubt , can you help? Ill Update the question, Jun 20, 2012 at 13:54
  • still related to this question? if no, you'd better create another question to avoid mixing different problems in one question.
    – LeleDumbo
    Jun 20, 2012 at 13:57
  • SELECT date,SEC_TO_TIME(SUM(TIME_TO_SEC(total_hours))) AS total FROM work_details WHERE employee_id = '28' AND DATE BETWEEN '2012-02-01' AND '2012-02-29' GROUP BY DATE ORDER BY DATE This Worked for me ,Thank you:) Jun 20, 2012 at 14:06
  • seems like you've figured the answer yourself, an accept wouldn't be rejected :)
    – LeleDumbo
    Jun 20, 2012 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.