views:

38

answers:

1

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

+2  A: 

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)

Philip Fourie
Just tested on our system and the query behaves as expected. OP should post a (none)working example, complete with creating the table and inserting records.
Lieven