Re: Determine the time difference from records in a select

From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: "Creager, Robert S" <CreagRS(at)LOUISVILLE(dot)STORTEK(dot)COM>, "'SQL - PGSQL'" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Determine the time difference from records in a select
Date: 2001-06-13 21:22:43
Message-ID: web-71614@davinci.ethosmedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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 in the FROM clause, which requires 7.1 or
greater.

SELECT agg_last.ip_addr, first_hit, last_hit, (last_hit- first_hit) AS
hit_interval
FROM
(SELECT ip_addr, max(hit_date) FROM hits GROUP BY ip_addr)
agg_last,
(SELECT ip_addr, min(hit_date) FROM hits GROUP BY ip_addr)
agg_first
WHERE agg_last.ip_addr = agg_first.ip_addr;

-Josh Berkus

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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message jeff 2001-06-13 21:23:14 RE: large going giving errors.
Previous Message Josh Berkus 2001-06-13 21:18:31 Re: Determine the time difference from records in a select