X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id A974953238 for ; Fri, 6 May 2005 05:04:39 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 64132-03 for ; Fri, 6 May 2005 08:04:33 +0000 (GMT) Received: from ausimss.pervasive.com (ausimss.pervasive.com [66.45.103.246]) by svr1.postgresql.org (Postfix) with ESMTP id D2DD752A74 for ; Fri, 6 May 2005 05:04:31 -0300 (ADT) Received: from ausbayes1.aus.pervasive.com ([172.16.8.6]) by ausimss.pervasive.com with InterScan Messaging Security Suite; Fri, 06 May 2005 03:04:29 -0500 Received: from BLRMAIL1.aus.pervasive.com ([10.3.16.60]) by ausbayes1.aus.pervasive.com with Microsoft SMTPSVC(5.0.2195.6713); Fri, 6 May 2005 03:04:28 -0500 content-class: urn:content-classes:message Subject: Re: select within aggregate? Date: Fri, 6 May 2005 13:34:26 +0530 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Message-ID: <02767D4600E59A4487233B23AEF5C59922C2AB@blrmail1.aus.pervasive.com> X-MS-Has-Attach: X-MimeOLE: Produced By Microsoft Exchange V6.0.6603.0 X-MS-TNEF-Correlator: Thread-Topic: [SQL] select within aggregate? Thread-Index: AcVSDUmRv9WrWI3IQfaer5p5CmQDSwABVGQg From: "Ramakrishnan Muralidharan" To: "Vortex" , X-OriginalArrivalTime: 06 May 2005 08:04:28.0746 (UTC) FILETIME=[3A17E2A0:01C55212] X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.071 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200505/66 X-Sequence-Number: 21379 Hi create table abc ( remote_host varchar(50),request_uri varchar(50),ts timestamp=20 ); SELECT abc.remote_host , c , abc.request_uri , a.t FROM abc , ( select = remote_host , count(*) as c , max( ts ) as t from abc group by = remote_host ) as a=20 where a.remote_host =3D abc.remote_host and abc.ts =3D a.t Regards, R.Muralidharan -----Original Message----- From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]On Behalf Of Vortex Sent: Friday, May 06, 2005 12:56 PM To: pgsql-sql@postgresql.org Subject: [SQL] select within aggregate? Hi!! Of course the following is possible in various ways but i wonder if there is an elegant and simple solution: A part from apache log: remote_host varchar(50) request_uri varchar(50) ts timestamp with time zone Now i want to see which host has done how many requests. This is not difficult: SELECT remote_host, count(*) FROM apache_log GROUP BY remote_host; But now i would like to add a column to the result which shows the most recent requested uri for each group. This sounds quite easy at first glance but i see no simple solution. I suppose with max(ts) i could acquire the time of the most recent request within the group, but how do i get the corresponding request_uri without doing subquerys or something like that? Thank you very much! Klaus ---------------------------(end of broadcast)--------------------------- TIP 2: you can get off all lists at once with the unregister command (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)