Home » SQL & PL/SQL » SQL & PL/SQL » average of time in HH:MM:SS format
|
|
|
|
|
Re: average of time in HH:MM:SS format [message #461269 is a reply to message #461264] |
Thu, 17 June 2010 05:23   |
 |
Michel Cadot
Messages: 68758 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Quote:column is declared as timestamp(6).
Quote: have declared my time data column as varchar2(10)..
which one is true?
Post a working Test case: create table and insert statements along with the result you want with these data then we will work with your table and data.
Before Please read OraFAQ Forum Guide, especially "How to format your post?" section.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code, use code tags and align the columns in result.
Use the "Preview Message" button to verify.
Also always post your Oracle version with 4 decimals.
Regards
Michel
[Updated on: Thu, 17 June 2010 05:40] Report message to a moderator
|
|
|
|
|
|
|
|
Re: average of time in HH:MM:SS format [message #461285 is a reply to message #461283] |
Thu, 17 June 2010 05:40   |
 |
Michel Cadot
Messages: 68758 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Michel Cadot wrote on Thu, 17 June 2010 12:23
Post a working Test case: create table and insert statements along with the result you want with these data then we will work with your table and data.
Before Please read OraFAQ Forum Guide, especially "How to format your post?" section.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code, use code tags and align the columns in result.
Use the "Preview Message" button to verify.
Also always post your Oracle version with 4 decimals.
Regards
Michel
[Updated on: Thu, 17 June 2010 05:40] Report message to a moderator
|
|
|
Re: average of time in HH:MM:SS format [message #461286 is a reply to message #461259] |
Thu, 17 June 2010 05:41   |
 |
Michel Cadot
Messages: 68758 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
shobhakashyapgmailcom wrote on Thu, 17 June 2010 12:11can u please tell me the sample sql query..how to convert it...
i tried with my code but getting error
my query
select AVG(to_number(substr(MY_COL,1,2) ) * 3600
+ to_number(substr(MY_COL,4,2) ) * 60
+ to_number(substr(MY_COL,7,2) ))
from MY_DATA
Which error? I don't see any error in your post.
Use SQL*Plus and copy and paste your session.
Regards
Michel
|
|
|
|
|
|
|
|
|
|
|
Re: average of time in HH:MM:SS format [message #461304 is a reply to message #461300] |
Thu, 17 June 2010 05:58   |
Solomon Yakobson
Messages: 3305 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Or, assuming minutes and seconds are always two digits and hours do not exceed 99:
select AVG(to_number(substr(lpad(MY_COL,8,'0'),1,2) ) * 3600
+ to_number(substr(lpad(MY_COL,8,'0'),4,2) ) * 60
+ to_number(substr(lpad(MY_COL,8,'0'),7,2) ))
from MY_DATA
SY.
P.S. It could be adjusted same way if hours can be N digit number.
|
|
|
Re: average of time in HH:MM:SS format [message #461364 is a reply to message #461304] |
Thu, 17 June 2010 09:39   |
_jum
Messages: 577 Registered: February 2008
|
Senior Member |
|
|
Would solve the problem this way:
--build a datetime from the given string
-- and format the date as seconds past midnight (0-86399)
WITH dattim AS
(SELECT to_date('05:12:45','HH24:MI:SS') tim FROM DUAL UNION ALL
SELECT to_date('13:36:01','HH24:MI:SS') FROM DUAL)
SELECT tim, to_char(tim,'SSSSS') ctim FROM dattim;
TIM CTIM
-----------------------------
01.06.2010 05:12:45 18765
01.06.2010 13:36:01 48961
--add and avg the seconds
WITH dattim AS
(SELECT to_date('05:12:45','HH24:MI:SS') tim FROM DUAL UNION ALL
SELECT to_date('13:36:01','HH24:MI:SS') FROM DUAL)
SELECT avg(to_number(to_char(tim,'SSSSS'))) atim FROM dattim;
ATIM
--------
33863
--build a date again
WITH dattim AS
(SELECT to_date('05:12:45','HH24:MI:SS') tim FROM DUAL UNION ALL
SELECT to_date('13:36:01','HH24:MI:SS') FROM DUAL)
SELECT to_date(avg(to_number(to_char(tim,'SSSSS'))),'SSSSS') avgtim FROM dattim;
avgtim
------------------
01.06.2010 09:24:23
This only works for times from ONE day. You should heed the advices to store the date not as a string, but as a genuin date!
[Updated on: Thu, 17 June 2010 09:39] Report message to a moderator
|
|
|
Re: average of time in HH:MM:SS format [message #461365 is a reply to message #461364] |
Thu, 17 June 2010 09:55   |
 |
Michel Cadot
Messages: 68758 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
I'm very fond of date function but I don't see any advantage to go to them here when a simple arithmetic computation as syakobson showed works.
SQL> WITH
2 dattim AS (
3 SELECT to_date('05:12:45','HH24:MI:SS') tim FROM DUAL UNION ALL
4 SELECT to_date('13:36:01','HH24:MI:SS') FROM DUAL
5 )
6 select AVG(to_number(substr(lpad(tim,8,'0'),1,2) ) * 3600
7 + to_number(substr(lpad(tim,8,'0'),4,2) ) * 60
8 + to_number(substr(lpad(tim,8,'0'),7,2) )) sec
9 from dattim
10 /
SEC
----------
3980
SQL> WITH
2 dattim AS (
3 SELECT to_date('05:12:45','HH24:MI:SS') tim FROM DUAL UNION ALL
4 SELECT to_date('13:36:01','HH24:MI:SS') FROM DUAL
5 ),
6 seconds as (
7 select AVG(to_number(substr(lpad(tim,8,'0'),1,2) ) * 3600
8 + to_number(substr(lpad(tim,8,'0'),4,2) ) * 60
9 + to_number(substr(lpad(tim,8,'0'),7,2) )) sec
10 from dattim
11 )
12 select to_char(trunc(sec/3600),'fm999999990')||':'||
13 to_char(trunc(mod(sec,3600)/60),'fm00')||':'||
14 to_char(mod(sec,60),'fm00') res
15 from seconds
16 /
RES
-------
1:06:20
Regards
Michel
|
|
|
|
Re: average of time in HH:MM:SS format [message #461369 is a reply to message #461367] |
Thu, 17 June 2010 10:08   |
cookiemonster
Messages: 13967 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
You're missing some to_char's, I get wildly different results:
SQL> WITH
2 dattim AS (
3 SELECT to_date('05:12:45','HH24:MI:SS') tim FROM DUAL UNION ALL
4 SELECT to_date('13:36:01','HH24:MI:SS') FROM DUAL
5 )
6 select AVG(to_number(substr(lpad(tim,8,'0'),1,2) ) * 3600
7 + to_number(substr(lpad(tim,8,'0'),4,2) ) * 60
8 + to_number(substr(lpad(tim,8,'0'),7,2) )) sec
9 from dattim;
SEC
----------
72001
SQL> WITH
2 dattim AS (
3 SELECT to_date('05:12:45','HH24:MI:SS') tim FROM DUAL UNION ALL
4 SELECT to_date('13:36:01','HH24:MI:SS') FROM DUAL
5 ),
6 seconds as (
7 select AVG(to_number(substr(lpad(tim,8,'0'),1,2) ) * 3600
8 + to_number(substr(lpad(tim,8,'0'),4,2) ) * 60
9 + to_number(substr(lpad(tim,8,'0'),7,2) )) sec
10 from dattim
11 )
12 select to_char(trunc(sec/3600),'fm999999990')||':'||
13 to_char(trunc(mod(sec,3600)/60),'fm00')||':'||
14 to_char(mod(sec,60),'fm00') res
15 from seconds;
RES
------------------
20:00:01
SQL> select sysdate from dual;
SYSDATE
---------------
20100617 154854
SQL>
|
|
|
Re: average of time in HH:MM:SS format [message #461370 is a reply to message #461367] |
Thu, 17 June 2010 10:08   |
 |
Michel Cadot
Messages: 68758 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
I was wrong to copy and paste your test case as it is not what OP has.
SQL> WITH
2 dattim AS (
3 SELECT '05:12:45' tim FROM DUAL UNION ALL
4 SELECT '13:36:01' FROM DUAL
5 )
6 select AVG(to_number(substr(lpad(tim,8,'0'),1,2) ) * 3600
7 + to_number(substr(lpad(tim,8,'0'),4,2) ) * 60
8 + to_number(substr(lpad(tim,8,'0'),7,2) )) sec
9 from dattim
10 /
SEC
----------
33863
1 row selected.
SQL>
SQL> WITH
2 dattim AS (
3 SELECT '05:12:45' tim FROM DUAL UNION ALL
4 SELECT '13:36:01' FROM DUAL
5 ),
6 seconds as (
7 select AVG(to_number(substr(lpad(tim,8,'0'),1,2) ) * 3600
8 + to_number(substr(lpad(tim,8,'0'),4,2) ) * 60
9 + to_number(substr(lpad(tim,8,'0'),7,2) )) sec
10 from dattim
11 )
12 select to_char(trunc(sec/3600),'fm999999990')||':'||
13 to_char(trunc(mod(sec,3600)/60),'fm00')||':'||
14 to_char(mod(sec,60),'fm00') res
15 from seconds
16 /
RES
------------------
9:24:23
1 row selected.
Regards
Michel
|
|
|
|
|
|
Goto Forum:
Current Time: Wed May 28 01:29:06 CDT 2025
|