Article View: comp.lang.pascal.delphi.databases
Article #83An alternate to GROUP BY???
From: Robert
Date: Thu, 28 Jun 2001 18:01
Date: Thu, 28 Jun 2001 18:01
57 lines
2809 bytes
2809 bytes
Hi, After much reading of posts in these newsgroups, I am at a quandary. Before launching into a long description, a simple example will probably do better: My table with sample records: CustNo ProdName Cost 123 Hammer 7.99 123 Saw 12.99 123 Paint Brush 2.49 123 Sand Paper 1.59 555 Spakeling 4.59 555 Drill Bit Set 15.99 I want an SQL statement to produce ONE record for each customer which lists the most expensive item he purchased. Example: CustNo ProdName Cost 123 Saw 12.99 555 Drill Bit Set 15.99 The SQL statement I tried was: SELECT CustNo, Max(Cost) AS MaxCost, ProdName FROM ORDERS GROUP BY CustNo I get the resulting error: "When GROUP BY exists, every simple field in projectors must be in GROUP BY." If I do end up putting the ProdName field in the GROUP BY clause, the result set end is NOT what I want. It looks like: SELECT CustNo, Max(Cost) AS MaxCost, ProdName FROM ORDERS GROUP BY CustNo, ProdName CustNo MaxCost Prodname 123 7.99 Hammer 123 2.49 Paint Brush 123 1.59 Sand Paper 123 12.99 Saw 555 15.99 Drill Bit Set 555 4.59 Spakeling Okay, I now realize that all non-aggregate fields must be in the GROUP BY clause, but it will ALWAYS not produce what I want. What alternate can I use to achieve the 2 record result set I am looking for? I must be overlooking something really simple. Thanks in advance, Pulling my Hair Out
Technical Details
Message-ID:
<7ab1d842.0106281001.52711997@posting.google.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!postnews1.google.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: ffff5,bd3b5dea6a171203,start X-Google-Attributes: gidffff5,public X-Google-Thread: 10de66,bd3b5dea6a171203,start X-Google-Attributes: gid10de66,public X-Google-ArrivalTime: 2001-06-28 11:01:14 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!postnews1.google.com!not-for-mail From: tallrobert@yahoo.com (Robert) Newsgroups: comp.lang.pascal.delphi.databases,borland.public.delphi.database.sqlservers Subject: An alternate to GROUP BY??? Date: 28 Jun 2001 11:01:13 -0700 Organization: http://groups.google.com/ Lines: 57 Message-ID: <7ab1d842.0106281001.52711997@posting.google.com> NNTP-Posting-Host: 216.227.118.61 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 993751274 8096 127.0.0.1 (28 Jun 2001 18:01:14 GMT) X-Complaints-To: groups-support@google.com NNTP-Posting-Date: 28 Jun 2001 18:01:14 GMT