473,395 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Help: need avg(timestamp1 - timestamp2), get type error

Hi -

I'm trying to query my database to find the min, max and average times
(in seconds, or minutes, or something) between two timestamps. I've
tried using:

Select avg(timestamp1 - timestamp2) from table;

I get an error saying that avg() expects a number, not an interval.
Is there a way (in a SQL stmt) to convert timestamps (or their
intervals, if they're different) so that I can run the avg, min, and
max functions?

I know that I could easily do this in C++ or Java, but I'm hoping to
do all of my calculations in SQL statements, since they will be faster
than in Java.

Thanks!
Ariant
Jul 19 '05 #1
3 10922

"Ariant" <ar******@earthlink.net> wrote in message
news:7a**************************@posting.google.c om...
Hi -

I'm trying to query my database to find the min, max and average times
(in seconds, or minutes, or something) between two timestamps. I've
tried using:

Select avg(timestamp1 - timestamp2) from table;

I get an error saying that avg() expects a number, not an interval.
Is there a way (in a SQL stmt) to convert timestamps (or their
intervals, if they're different) so that I can run the avg, min, and
max functions?

I know that I could easily do this in C++ or Java, but I'm hoping to
do all of my calculations in SQL statements, since they will be faster
than in Java.

Thanks!
Ariant


Try the documentation. If you subtract 1 o'clock from 2 o"clock what do you
get? An interval of an hour!
Jim
Jul 19 '05 #2
VC
Hello,

Assuming you have a table like this:

create table t1(x timestamp, y timestamp)

... the query would be:

select avg (to_date(to_char(x, 'mm/dd/yyyy hh24:mi:ss'), 'mm/dd/yyyy
hh24:mi:ss') -
to_date(to_char(y, 'mm/dd/yyyy hh24:mi:ss'), 'mm/dd/yyyy
hh24:mi:ss')) * 24*60*60 avg_seconds
from t1
You need to convert timestamp-> char->date.
Rgds.

VC

"Ariant" <ar******@earthlink.net> wrote in message
news:7a**************************@posting.google.c om...
Hi -

I'm trying to query my database to find the min, max and average times
(in seconds, or minutes, or something) between two timestamps. I've
tried using:

Select avg(timestamp1 - timestamp2) from table;

I get an error saying that avg() expects a number, not an interval.
Is there a way (in a SQL stmt) to convert timestamps (or their
intervals, if they're different) so that I can run the avg, min, and
max functions?

I know that I could easily do this in C++ or Java, but I'm hoping to
do all of my calculations in SQL statements, since they will be faster
than in Java.

Thanks!
Ariant

Jul 19 '05 #3
Thanks VC -

But I have one last problem, I've now realized I need microsecond
precision on my calculations (i'm using a timestamp w/ 6 decimal
places for the second). I can't seem to find a way to do that. also,
is there a way to do an "assignment" (I dont have write privleges to
the DB) so I don't have to calculate timestamp1-timestamp2 more than
once (if i want to use it a few times in a query)?

Thanks so much!
Ariant

"VC" <bo*******@hotmail.com> wrote in message news:<c4JNb.77583$xy6.136006@attbi_s02>...
Hello,

Assuming you have a table like this:

create table t1(x timestamp, y timestamp)

.. the query would be:

select avg (to_date(to_char(x, 'mm/dd/yyyy hh24:mi:ss'), 'mm/dd/yyyy
hh24:mi:ss') -
to_date(to_char(y, 'mm/dd/yyyy hh24:mi:ss'), 'mm/dd/yyyy
hh24:mi:ss')) * 24*60*60 avg_seconds
from t1
You need to convert timestamp-> char->date.
Rgds.

VC

"Ariant" <ar******@earthlink.net> wrote in message
news:7a**************************@posting.google.c om...
Hi -

I'm trying to query my database to find the min, max and average times
(in seconds, or minutes, or something) between two timestamps. I've
tried using:

Select avg(timestamp1 - timestamp2) from table;

I get an error saying that avg() expects a number, not an interval.
Is there a way (in a SQL stmt) to convert timestamps (or their
intervals, if they're different) so that I can run the avg, min, and
max functions?

I know that I could easily do this in C++ or Java, but I'm hoping to
do all of my calculations in SQL statements, since they will be faster
than in Java.

Thanks!
Ariant

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jerry | last post by:
Hi All I would very much appreciate your help: I have two scripts alternating in the background triggering themselves mutually. Here is how: 1.) Script A does something and then calls Script...
24
by: Xah Lee | last post by:
What is Expresiveness in a Computer Language 20050207, Xah Lee. In languages human or computer, there's a notion of expressiveness. English for example, is very expressive in manifestation,...
1
by: Jie | last post by:
Hi All, My tree view (Microsoft.Web.UI.WebControls.dll) has been running well till I got this error in design mode The following is the detail message: Resource file name:...
3
by: Jeff | last post by:
OS: XPpro sp2 Visual Studio 2003 : 7.1.3088 ..NET Framework 1.1 (1.1.4322 sp1) When I in VS tryes to create an ASP.NET app I get this error message: "Visual Studio .NET has detected that hte...
2
by: Keith Bottner | last post by:
I just reinstalled my system and am in the process of getting PostgreSQL up and running again. During compilation of Postgres I received the following error: .... checking for main in...
3
by: Ramesh Dodamani | last post by:
Environment: XP Pro, VS.Net 2003, .Net 1.1.4322 with SP1 & KB Hotfix 886903 P4 2.2GHz, 1 GB RAM My system was working fine till a few weeks back when I started seeing the following errors. ...
6
by: isaac2004 | last post by:
hello i am getting a type mismatch for a page i am trying to write. any help would be great source code <%@ Language=VBScript %> <% Option Explicit %> <!--#include...
3
by: Ariant | last post by:
Hi - I'm trying to query my database to find the min, max and average times (in seconds, or minutes, or something) between two timestamps. I've tried using: Select avg(timestamp1 -...
3
by: Chrizo | last post by:
Hi, I am new to C++ and I cannot seem to figure out what is wrong with my code. It looks fine to me but when I try to create an object of class Location I get this error which I do not understand...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.