X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 7E2D09DCA59 for ; Fri, 16 Dec 2005 12:04:58 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 60622-07 for ; Fri, 16 Dec 2005 12:04:57 -0400 (AST) X-Greylist: delayed 00:22:09.338958 by SQLgrey- Received: from lon1-mail-2.visp.demon.net (lon1-mail-2.visp.demon.net [193.195.70.5]) by postgresql.org (Postfix) with ESMTP id D94549DC84C for ; Fri, 16 Dec 2005 12:04:55 -0400 (AST) Received: from xphome (host-72a-th-m-86.dial.beeb.net [62.56.16.86] (may be forged)) by lon1-mail-2.visp.demon.net (MOS 3.5.8-GR) with ESMTP id DDK78287; Fri, 16 Dec 2005 15:42:33 GMT From: "Michael Farewell" To: Subject: Problem obtaining MAX values FROM TABLE Date: Fri, 16 Dec 2005 15:42:34 -0000 Message-ID: <000701c60257$567d9980$0100a8c0@xphome> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.6626 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 Importance: Normal X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[none] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200512/186 X-Sequence-Number: 23728 I am having a problem with a query, I have a view which produces = something like this: b_id company_name product_count product_type=20 ------------------------------------------------ 29 "company 1" 1 "a" 29 "company 2" 1 "b" 29 "company 3" 3 "a" 27 "company 4" 1 "c" 27 "company 4" 4 "d" 24 "company 5" 3 "a" 24 "company 5" 5 "c" 24 "company 5" 2 "d" ------------------------------------------------- I need to write a query which returns each company together with the highest product_count and its associated product type so the result = should=20 look like this: b_id company_name product_count product_type=20 ------------------------------------------------ 29 "company 1" 1 "a" 29 "company 2" 1 "b" 29 "company 3" 3 "a" 27 "company 4" 4 "d" 24 "company 5" 5 "c" ------------------------------------------------- I have tried the following query: SELECT company, MAX(type_count), product_type FROM = buyer_product_frequencies GROUP BY company, product_type But in this case it just produces the same results as are in the first table. If I drop product_type from the query I get the right result but = I don't have the product type which I need. This problem is driving me = mad! so any assistance would be greatly appreciated. Many thanks, Mike Farewell