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 39A8753065 for ; Fri, 12 Aug 2005 15:03:24 -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 38561-01 for ; Fri, 12 Aug 2005 18:03:21 +0000 (GMT) Received: from wolff.to (wolff.to [66.93.197.194]) by svr1.postgresql.org (Postfix) with SMTP id DD86153079 for ; Fri, 12 Aug 2005 15:03:20 -0300 (ADT) Received: (qmail 2574 invoked by uid 500); 12 Aug 2005 18:04:55 -0000 Date: Fri, 12 Aug 2005 13:04:55 -0500 From: Bruno Wolff III To: Lane Van Ingen Cc: pgsql-sql@postgresql.org Subject: Re: SQL Newbie Message-ID: <20050812180455.GA32286@wolff.to> Mail-Followup-To: Bruno Wolff III , Lane Van Ingen , pgsql-sql@postgresql.org References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 tagged_above=0 required=5 tests=[AWL=0.006] X-Spam-Level: X-Archive-Number: 200508/75 X-Sequence-Number: 22303 > Data in speed history looks like this: > interface_id updated_time speed > 1 2005-08-11 08:10:23 450112 > 1 2005-08-11 10:53:34 501120 <--- > 1 2005-08-11 10:58:11 450112 > 2 2005-08-11 08:10:23 450112 <--- > 2 2005-08-11 11:00:44 350234 > 3 2005-08-11 08:10:23 450112 <--- > The rows of speed_history I want back are marked above with ' <--- '. > > Query results should look like: > interface.interface_id > interface.link_description > speed_history.updated_time > speed_history.speed The main idea is to join a select of max speeds grouped by interface_id to the interface table. If the query runs too slow, then you may get better performance having an index on (interface_id, speed) and using subselects. In this case you want to select information about all of interfaces and then have one of the columns be a subselect that selects one (using limit) speed from rows that have a matching interface_id ordered by interface_id desc, speed desc. This combination of limit and order by will be faster than using max.