Click here to Skip to main content
16,022,189 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How I sum or aggregate time data in SQL server, from cast

00:34:32
00:17:15
--------------------
TOTAL = 01:26:19.0000000
should : 00:51:47.0000000

What I have tried:

SELECT DATEADD(hh, t.TotalHours, DATEADD(n, t.TotalMinutes, DATEADD(ss, t.TotalSeconds, CAST('00:00:00' AS time)))) AS TotalTime
FROM (

SELECT SUM(DATEPART(hh, convert(varchar, CAST((tsp.wa1 - (ta.regDate+ta.regTime) ) as time(0)), 14))) AS TotalHours,
SUM(DATEPART(n, convert(varchar, CAST((tsp.wa1 - (ta.regDate+ta.regTime) ) as time(0)), 14))) AS TotalMinutes,
SUM(DATEPART(ss, convert(varchar, CAST((tsp.wa1 - (ta.regDate+ta.regTime) ) as time(0)), 14))) AS TotalSeconds

FROM tbantrian ta WITH (NOLOCK)
LEFT JOIN tbrawatjalan trj WITH ( NOLOCK ) ON ta.struckNo = trj.struk
LEFT JOIN tbcustomer tc WITH ( NOLOCK ) ON ta.custCode = tc.custCode
LEFT JOIN tbdoctor td WITH ( NOLOCK ) ON ta.doctor = td.docCode
LEFT JOIN tbSoapPoli tsp WITH ( NOLOCK ) ON trj.tranCode = tsp.tranCode
LEFT JOIN tbSoapPoliPerawat tspp WITH ( NOLOCK ) ON trj.tranCode = tspp.tranCode
LEFT JOIN tbResepDokter trd WITH ( NOLOCK ) ON trj.tranCode = trd.tranCode
LEFT JOIN tbpoli tp WITH ( NOLOCK ) ON ta.poly = tp.polyCode
LEFT JOIN tbuser tu WITH ( NOLOCK ) ON ta.doctor = tu.docCode

Where ta.regDate >= '2024-07-11 00:00:00' and ta.regDate <= '2024-07-11 01:59:59'
and ta.status <> 'Batal'and ta.status <> 'Antri' and ta.status <> 'Proses' and ta.status <> 'Antri'
and tp.polyCode = 'POL043'
and tsp.ketPasien ='Esklusi'


) AS t;
Posted
Comments
[no name] 30-Jul-24 23:41pm    
Suika game has bright, cute 2D graphics, with detailed and lovely designed characters, creating a friendly and fun feeling for players.

1 solution

Convert the TotalHours and TotalMinutes values to seconds, then do all the arithmetic to get a full total of the number of seconds.
You can then convert that back to a number of seconds, minutes, and hours using the modulus (%) and divide (/) operators

I'd suggest writing an SQL function to do the conversions as it'#s make your code a lot more readable (and easier to test).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900