开发者

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


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜