Double of the common problem

Table 1

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

WorkTime data type - ** 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 displays "double total total time"

Example

From table1, I want to calculate the total runtime, when I run the above query, it shows

ID WorkTime

001 21:40:00
002...,

But it should look like this:

ID Worktime

001 10:50:00
...,

Avoiding double total working hours. How to change my request.

Query Request Help

+3
source share
1 answer

After creating the tables and adding the data as described in the question, I got the expected answer 10:50:00. Not sure why it doesn't work for OP ...

, WorkTime varchar int. .

, . ( , )

+2

Source: https://habr.com/ru/post/1735864/


All Articles