Re: how to use an aggregate function

From: David W Noon <dwnoon(at)spamtrap(dot)ntlworld(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: how to use an aggregate function
Date: 2003-05-17 17:14:09
Message-ID: 287ip-3hb.ln1@my-pc.ntlworld.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Saturday 17 May 2003 17:18 in
<de0fa271(dot)0305170818(dot)5972e18e(at)posting(dot)google(dot)com>, jmsmithe
(ggunning(at)esatclear(dot)ie) wrote:

[snip]
> How would I
> List the name of customers who have paid an average of more then $6.
> Produce a listing Cname, Avg_Paid

In your Item table, UnitPrice is an INT. Please choose a better data type
for monetary values, as an average is problematic in an integer domain: the
average is usually not an element of that domain. Something like
DECIMAL(6,2) could be good.

> All I can think of is this.
>
> SELECT Customer.Cname, AVG(Item.UnitPrice) AS Avg_Paid
> FROM Customer
> JOIN AOrder
> ON Customer.CustomerID = AOrder.CustomerID
> JOIN OrderItem
> ON AOrder.AOrderID = OrderItem.AOrderID
> JOIN Item
> ON OrderItem.ItemID = Item.ItemID
> GROUP BY Customer.Cname

HAVING Avg_paid > 6.00

> ;

Try this after changing the data type on Item.UnitPrice.

--
Regards,

Dave
======================================================
dwnoon(at)spamtrap(dot)ntlworld(dot)com (David W Noon)
Remove spam trap to reply via e-mail.
RLU#314465
======================================================

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Stephan Szabo 2003-05-17 17:41:10 Re: COALESCE in ORDER BY...
Previous Message Terry Yapt 2003-05-17 16:40:01 COALESCE in ORDER BY...