Received: from davinci.ethosmedia.com (davinci.ethosmedia.com [209.10.40.250]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f5DLMoE18696 for ; Wed, 13 Jun 2001 17:22:50 -0400 (EDT) (envelope-from josh@agliodbs.com) Received: from [209.10.40.250] (account ) by davinci.ethosmedia.com (CommuniGate Pro WebUser 3.4.1) with HTTP id 71614; Wed, 13 Jun 2001 14:22:43 -0700 From: "Josh Berkus" Subject: Re: Determine the time difference from records in a select To: "Creager, Robert S" , "'SQL - PGSQL'" X-Mailer: CommuniGate Pro Web Mailer v.3.4.1 Date: Wed, 13 Jun 2001 14:22:43 -0700 Message-ID: In-Reply-To: <10FE17AD5F7ED31188CE002048406DE8514DE0@lsv-msg06.stortek.com> MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit X-Archive-Number: 200106/203 X-Sequence-Number: 3459 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