🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.pascal.delphi.databases
2 messages
2 total messages Started by Robert Thu, 28 Jun 2001 18:01
An alternate to GROUP BY???
#83
Author: Robert
Date: Thu, 28 Jun 2001 18:01
57 lines
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
Re: An alternate to GROUP BY???
#82
Author: Ralf Jansen
Date: Fri, 29 Jun 2001 11:12
29 lines
1773 bytes
tallrobert@yahoo.com (Robert) wrote in message news:<7ab1d842.0106281001.52711997@posting.google.com>...
> 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:
> 
> ....
> 
> Thanks in advance,
> Pulling my Hair Out

Hello,

the 'group by' clause won't help you here ,because you don't want to
have a grouped result. You want to have a concrete record with a
specific (max) value.

One way to do this is to use a subselect for filtering your result to
the
records with the max cost.  

SELECT CustNo, Cost, ProdName 
  FROM ORDERS o
 WHERE Cost = (select max(Cost) from ORDERS i where i.CustNo =
o.CustNo)

Good Luck,
Ralf
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads