pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Determine the time difference from records in a select
3+ messages / 2 participants
[nested] [flat]

* Determine the time difference from records in a select
@ 2001-06-13 18:32  Creager, Robert S <CreagRS@LOUISVILLE.STORTEK.COM>
  0 siblings, 2 replies; 3+ messages in thread

From: Creager, Robert S @ 2001-06-13 18:32 UTC (permalink / raw)
  To: pgsql-sql


I apologize in advance, 'cause I think this was addressed a couple of months
ago, but I cannot find it in the archives.

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.

Help?
Rob

Robert Creager
Senior Software Engineer
Client Server Library
303.673.2365 V
303.661.5379 F
888.912.4458 P
StorageTek
INFORMATION made POWERFUL





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Determine the time difference from records in a select
@ 2001-06-13 21:18  Josh Berkus <josh@agliodbs.com>
  parent: Creager, Robert S <CreagRS@LOUISVILLE.STORTEK.COM>
  1 sibling, 0 replies; 3+ messages in thread

From: Josh Berkus @ 2001-06-13 21:18 UTC (permalink / raw)
  To: Creager, Robert S <CreagRS@LOUISVILLE.STORTEK.COM>; 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 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



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Determine the time difference from records in a select
@ 2001-06-13 21:22  Josh Berkus <josh@agliodbs.com>
  parent: Creager, Robert S <CreagRS@LOUISVILLE.STORTEK.COM>
  1 sibling, 0 replies; 3+ messages in thread

From: Josh Berkus @ 2001-06-13 21:22 UTC (permalink / raw)
  To: Creager, Robert S <CreagRS@LOUISVILLE.STORTEK.COM>; 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@agliodbs.com
   and data management solutions       (415) 565-7293
  for law firms, small businesses        fax 621-2533
    and non-profit organizations.      San Francisco



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2001-06-13 21:22 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-06-13 18:32 Determine the time difference from records in a select Creager, Robert S <CreagRS@LOUISVILLE.STORTEK.COM>
2001-06-13 21:18 ` Josh Berkus <josh@agliodbs.com>
2001-06-13 21:22 ` Josh Berkus <josh@agliodbs.com>

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