Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double of Total Problem

Table1

ID  |  WorkTime
-----------------
001 |  10:50:00
001 |  00:00:00
002 |  ....

WorkTime Datatype is **varchar*(.

SELECT ID, 
       CONVERT(varchar(10), TotalSeconds1 / 3600) + ':' + RIGHT('00' + CONVERT(varchar(2), (TotalSeconds1 - TotalSeconds1 / 3600 * 3600) / 60), 2) + ':' + RIGHT('00' + CONVERT(varchar(2), TotalSeconds1 - (TotalSeconds1 / 3600 * 3600 + (TotalSeconds1 - TotalSeconds1 / 3600 * 3600) / 60 * 60)), 2) AS TotalWork 
From  ( SELECT ID, 
               SUM(DATEDIFF(second, CONVERT(datetime, '1/1/1900'), 
               CONVERT(datetime, '1/1/1900 ' + WorkTime))) AS TotalSeconds1 
          FROM table1 
      group by ID) AS tab1 
where id = '001'

The above Query is showing "double the total of time"

For Example

From table1 i want to calculate the total WorkTime, when i run the above query it is showing

ID WorkTime

001 21:40:00
002...,

But it should show like this

ID Worktime

001 10:50:00
...,

How to avoid the double total of worktime. How to modify my query.

Need Query Help

like image 426
Gopal Avatar asked Nov 19 '25 00:11

Gopal


1 Answers

After creating tables and adding data as described in the question, I still got the expected answer of 10:50:00. Not sure why it is not working for the OP...

To simplify the query and to enhance performance it might be better to change the type definition for WorkTime from varchar to int. Then save the work interval as total seconds.

It would then be simple to sum the interval amounts and then perform a display format on the final value. (Preferably in the application and not in the database)

like image 192
Philip Fourie Avatar answered Nov 20 '25 13:11

Philip Fourie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!