X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 6A7749FA42B for ; Wed, 21 Jun 2006 11:56:07 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 99799-08 for ; Wed, 21 Jun 2006 11:55:55 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from wr-out-0506.google.com (wr-out-0506.google.com [64.233.184.230]) by postgresql.org (Postfix) with ESMTP id AFD7B9FA19B for ; Wed, 21 Jun 2006 11:55:55 -0300 (ADT) Received: by wr-out-0506.google.com with SMTP id i7so172643wra for ; Wed, 21 Jun 2006 07:55:54 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=Na9hZfMY+7wJdADIp4eCmV4tZ79AQxTTMBAb/61Nrsm6KEKLPEBnSUvrxN9ySGiv/StBd7SV5cc3aj31KK70f6uoPn7T1BpFfHxvPEE5WoJovc7MnOE4FZvINGQA+1YJDRIWcrR2sBNj1/5boAFPzqaEpOjdimYNgXPOk9o7MdY= Received: by 10.54.152.12 with SMTP id z12mr832607wrd; Wed, 21 Jun 2006 07:55:54 -0700 (PDT) Received: by 10.54.128.14 with HTTP; Wed, 21 Jun 2006 07:55:54 -0700 (PDT) Message-ID: Date: Wed, 21 Jun 2006 10:55:54 -0400 From: "Worky Workerson" To: pgsql-sql@postgresql.org Subject: Displaying first, last, count columns MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/235 X-Sequence-Number: 25471 I'm having a bit of a brain freeze and can't seem to come up with decent SQL for the following problem: I have a table "t" of the form "time_occurred TIMESTAMP, prog_data VARCHAR" and would like to create a query that outputs something of the form "first_seen, last_seen, count, prog_data". I have the current query which gets the first_seen and last_seen via subqueries, ala SELECT t1.time_occurred AS first_seen, t2.time_occurred AS last_seen, t3.count, t1.prog_data FROM t AS t1, t AS t2 WHERE t1.prog_data = t2.prog_data AND t1.time_occurred IN (SELECT min(time_occurred) FROM t WHERE prog_data = t1.prog_data) AND t2.time_occurred IN (SELECT max(time_occurred) FROM t WHERE prog_data = t1.prog_data) but I can't seem to work out how to get the count of all the records that have. I figure that this is probably a relatively common idiom ... can anyone suggest ways to go about doing this. Also, the performance of this is pretty horrible, but I figure that creating a column on t.prog_data should speed things up noticably, right? Thanks!