Re: Determine the time difference from records in a select - Mailing list pgsql-sql

From Josh Berkus
Subject Re: Determine the time difference from records in a select
Date
Msg-id web-71610@davinci.ethosmedia.com
Whole thread Raw
In response to Determine the time difference from records in a select  ("Creager, Robert S" <CreagRS@LOUISVILLE.STORTEK.COM>)
List pgsql-sql
Robert:

> I have a firewall hit database which keeps the date of the hit and
> the
> offenders IP (among other items).  I would like to be able to execute
> a
> select which would show each unique IP and the time difference from
> the
> latest hit to the first hit.  I can do this in scripts, but was
> wondering if
> there is a pure SQL query/queries way to accomplish this.

You need to use a subselect, which means you need to be running 7.1 or
better.  Here's the basic idea:

SELECT agg_last.ip_address, first_hit, last_hit, (last_hit - first_hit)
AS hit_interval FROM
(SELECT ip_address, min(hit_date) as first_hit 



______AGLIO DATABASE SOLUTIONS___________________________                                      Josh Berkus Complete
informationtechnology      josh@agliodbs.com  and data management solutions       (415) 565-7293 for law firms, small
businesses       fax 621-2533   and non-profit organizations.      San Francisco
 


pgsql-sql by date:

Previous
From:
Date:
Subject: RE: large going giving errors.
Next
From: Tom Lane
Date:
Subject: Re: large going giving errors.