pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: Creager, Robert S <CreagRS@LOUISVILLE.STORTEK.COM>
To: 'SQL - PGSQL' <pgsql-sql@postgresql.org>
Subject: Re: Determine the time difference from records in a select
Date: Wed, 13 Jun 2001 14:22:43 -0700
Message-ID: <web-71614@davinci.ethosmedia.com> (raw)
In-Reply-To: <10FE17AD5F7ED31188CE002048406DE8514DE0@lsv-msg06.stortek.com>

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@agliodbs.com
   and data management solutions       (415) 565-7293
  for law firms, small businesses        fax 621-2533
    and non-profit organizations.      San Francisco



view thread (3+ messages)

Message-ID: <web-71614@davinci.ethosmedia.com>
Permalink:  ../web-71614@davinci.ethosmedia.com/
Also on:    postgresql.org/message-id/web-71614@davinci.ethosmedia.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: josh@agliodbs.com, CreagRS@LOUISVILLE.STORTEK.COM
  Subject: Re: Determine the time difference from records in a select
  In-Reply-To: <web-71614@davinci.ethosmedia.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox