Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id E24511337BD4 for ; Fri, 8 Apr 2011 07:31:32 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 96880-05 for ; Fri, 8 Apr 2011 10:31:25 +0000 (UTC) X-Greylist: delayed 00:10:17.830602 by SQLgrey-1.7.6 Received: from forward17.mail.yandex.net (forward17.mail.yandex.net [95.108.253.142]) by mail.postgresql.org (Postfix) with ESMTP id 605AA1337BE6 for ; Fri, 8 Apr 2011 07:31:25 -0300 (ADT) Received: from web25.yandex.ru (web25.yandex.ru [95.108.253.234]) by forward17.mail.yandex.net (Yandex) with ESMTP id 05CD010626C0; Fri, 8 Apr 2011 14:21:04 +0400 (MSD) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1302258064; bh=qpwYklhKIWuyGeTT90DPeIcKbKJbNXP9Usq8uu+8BGc=; h=From:To:Cc:In-Reply-To:References:Subject:MIME-Version:Message-Id: Date:Content-Transfer-Encoding:Content-Type; b=AqmlcnYlR1anRFfnKZzv/1j/wGyLX0bluLJHAVRvSNBNxNhZZ+cH0Yab+Qg8RvzJj XM1t/ObqjmujjMUoFii7Rs3s9uncJ6mX0zOMkLKwJdx2Lg5EOjTn9dqu1Iigssfhx3 jV7cx0Tf+YfWLGhgVlmYthhMoSq9RCz8u0gzE5wM= Received: from localhost (localhost.localdomain [127.0.0.1]) by web25.yandex.ru (Yandex) with ESMTP id E8C966BF0001; Fri, 8 Apr 2011 14:21:03 +0400 (MSD) Received: from [2.94.17.242] ([2.94.17.242]) by mail.yandex.ru with HTTP; Fri, 08 Apr 2011 14:21:02 +0400 From: msi77 To: Mark Fenbers Cc: pgsql-sql@postgresql.org In-Reply-To: <4D94E0CB.4020309@noaa.gov> References: <4D94E0CB.4020309@noaa.gov> Subject: Re: Aggregates puzzle MIME-Version: 1.0 Message-Id: <400201302258063@web25.yandex.ru> Date: Fri, 08 Apr 2011 14:21:02 +0400 X-Mailer: Yamail [ http://yandex.ru ] 5.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=koi8-r X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=3.824 tagged_above=-5 required=5 tests=BAYES_20=-0.001, FREEMAIL_ENVFROM_END_DIGIT=1.553, FREEMAIL_FROM=0.001, FSL_RU_URL=2.271 X-Spam-Level: *** X-Archive-Number: 201104/6 X-Sequence-Number: 35253 Try this: select * from flood_ts F join (SELECT lid, MAX(value) AS mvalue, event_id FROM flood_ts GROUP BY lid, event_id) X on F.lid = X.lid and value = mvalue and X.event_id = F.event_id 01.04.2011, 00:15, "Mark Fenbers" : > SQL gurus, > > I have a table with 4 columns: šlid(varchar), value(float), > obstime(datetime), event_id(integer) > > I want to find the MAX(value) and the time and date that it occurred > (obstime) in each group of rows where the lid and event_id are the > same. šWhat I have works correctly in identifying the MAX(value) for the > given group, but I'm having trouble getting the corresponding obstime to > be reported along with it. > > Here's the SQL I have: > > SELECT lid, MAX(value), event_id > FROM flood_ts > GROUP BY lid, event_id > ORDER BY lid; > > If I add "obstime" to the SELECT list, then I need to add "value" to the > GROUP BY clause, which makes the MAX(value) function report *each row* > as a maximum. > > So, how can I revise my SQL to report the obstime that the MAX(value) > occurred? > > Any help is sincerely appreciated. > > Mark > > -- > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql