Re: select within aggregate? - Mailing list pgsql-sql

From Ramakrishnan Muralidharan
Subject Re: select within aggregate?
Date
Msg-id 02767D4600E59A4487233B23AEF5C59922C2AB@blrmail1.aus.pervasive.com
Whole thread Raw
In response to select within aggregate?  (Vortex <vortex25@gmx.de>)
List pgsql-sql
Hi

create table abc
(
remote_host    varchar(50),request_uri    varchar(50),ts timestamp
);

SELECT abc.remote_host ,  c , abc.request_uri , a.t FROM abc , ( select remote_host , count(*) as c , max( ts ) as t
fromabc group by remote_host ) as a  
where a.remote_host = abc.remote_host and abc.ts = 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)


pgsql-sql by date:

Previous
From: "Ramakrishnan Muralidharan"
Date:
Subject: Re: trigger/rule question
Next
From: a3a18850@telus.net
Date:
Subject: Re: HELP: aggregating insert rule for multirow inserts.