0

How can get sum of time values stored as hh:mm format in a datetime field in SQLite? I have tried solution given at sqlite: how to add total time hh:mm:ss where column datatype is DATETIME? and it gives me same result as I get in Excel. But if I count manually one by one, the answer is 44 hours and 49 minutes. But both, excel and the query derived from the solution given at above URL give the answer as 20 hours and 49 minutes. I am including the query which I use.

Query:

select time(sum(strftime('%s',appUsageTime)-strftime('%s','00:00')),'unixepoch') 
from offlineAppsUsageHistory 
WHERE appId=3;

Unable to add Excel screenshot but the time values in the appUsageTime field are as follows:

01:27
01:10
01:21
00:54
01:28
01:53
01:29
03:30
02:11
03:38
02:40
02:07
03:04
02:42
02:24
02:16
02:05
01:46
02:41
01:13
02:07
00:43
Nick
  • 138,499
  • 22
  • 57
  • 95

1 Answers1

0

Because your sum exceeds 24 hours, time just returns the part that is not a whole day. Thus you get a result of 20:49 instead of 44:49 as 20:49 is 44:49 - 24:00. One way to display the total hours and minutes would be to use printf on the sum instead:

SELECT printf('%d:%02d', totsec / 3600, (totsec % 3600) / 60) AS total
FROM (
  SELECT sum(strftime('%s',appUsageTime)-strftime('%s','00:00')) AS totsec
  FROM offlineAppsUsageHistory 
  WHERE appId=3
) h

Output:

total
44:49

Demo on dbfiddle

Nick
  • 138,499
  • 22
  • 57
  • 95