Home » SQL & PL/SQL » SQL & PL/SQL » average of time in HH:MM:SS format
average of time in HH:MM:SS format [message #461254] |
Thu, 17 June 2010 04:59  |
shobhakashyapgmailcom
Messages: 58 Registered: June 2010 Location: India
|
Member |

|
|
hi
can any one tell me how to calculate the average of the time in th e HH:MM:SS format stored database table..
column contains hundreds of time values and need to table the avergae of it
my col look like,and column is declared as timestamp(6).
MY COL
------
1:13:00
1:06:00
0:43:00
0:47:00
0:32:00
0:19:00
0:39:00
0:46:00
0:56:00
1:39:00
1:40:00
0:17:00
0:30:00
0:40:00
0:30:00
21:08:00
|
|
|
|
|
|
|
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: 68757 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 #461273 is a reply to message #461264] |
Thu, 17 June 2010 05:25   |
JRowbottom
Messages: 5933 Registered: June 2006 Location: Sunny North Yorkshire, ho...
|
Senior Member |
|
|
Quote:i have declared my time data column as varchar2(10)..
Well, that was a very silly thing to do if you want to store dates in it.
You can use the same solution, but you'll have to wrap each reference to your column with a TO_DATE(...) to convert the column value to a date.
|
|
|
|
|
|
|
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: 68757 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: 68757 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 #461300 is a reply to message #461296] |
Thu, 17 June 2010 05:52   |
Solomon Yakobson
Messages: 3305 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Roachcoach wrote on Thu, 17 June 2010 06:48From that error it sounds like you're not converting the field properly.
Obviously:
> 0:47:00
and
> to_number(substr(MY_COL,1,2) )
do not work together. It should be adjusted with INSTR, o better using regular expressions.
SY.
|
|
|
|
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: 68757 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: 13966 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: 68757 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
|
|
|
|
|
Re: average of time in HH:MM:SS format [message #461445 is a reply to message #461431] |
Fri, 18 June 2010 00:55  |
 |
Michel Cadot
Messages: 68757 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
shobhakashyapgmailcom wrote on Fri, 18 June 2010 06:11the above code u mensioned was only which calculates 2 dates avg i think..but i have a data of more than 1000 rows and the column is declared as varchar2(10).
for your code to find the avg of two num am getting ora 01722 invalid number error
plzz help me to resolve
You refuse to post any test case and follow the guide so:
- Fix your data.
- Modify the query to fit your need.
- Make a little effort to do your job yourself.
The query works any number of rows.
Regards
Michel
|
|
|
Goto Forum:
Current Time: Sun Apr 27 02:49:52 CDT 2025
|