pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jean-Luc Lachance <jllachan@nsd.ca>
To: pgsql-sql@postgresql.org
Subject: Re: maximum of sums
Date: Thu, 21 Feb 2002 16:55:04 -0500
Message-ID: <3C756CB8.74537FBC@nsd.ca> (raw)
References: <3c71e972$0$16639$afc38c87@news.optusnet.com.au>

Rob,

How about 

select prod_no, sum( quantity) from orders group by prod_no order by 2
desc limit 1;

JLL


Rob wrote:
> 
> Howdy all!
> 
> Let's say we have a product orders table like this:
> 
> SQL> select * from orders;
> 
>  ORDER_NO   PROD_NO  QUANTITY
> --------- --------- ---------
>         1         2         3
>         2         2         3
>         3         3         3
>         4         1         4
>         5         3         8
>         6         2         7
>         7         1         6
>         8         1         3
>         9         3         3
>        10         2         4
> 
> 10 rows selected.
> 
> SQL>
> 
> I want to select the prod_no and sum (quantity) for the product with the max sum
> (quantity).
> 
> I have this so far:
> SQL> select
>   2     max (sumamt) as maximum
>   3  from
>   4     (select
>   5        sum (orders.quantity) as sumamt
>   6      from
>   7        orders
>   8      group by
>   9        orders.prod_no);
> 
>   MAXIMUM
> ---------
>        17
> 
> SQL>
> 
> But how can I get the matching prod_id?
> 
> Any help would be much appreciated!
> 
> Rob
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly



view thread (3+ messages)

Message-ID: <3C756CB8.74537FBC@nsd.ca>
Permalink:  ../3C756CB8.74537FBC@nsd.ca/
Also on:    postgresql.org/message-id/3C756CB8.74537FBC@nsd.ca

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: jllachan@nsd.ca
  Subject: Re: maximum of sums
  In-Reply-To: <3C756CB8.74537FBC@nsd.ca>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox