Skip site navigation (1) Skip section navigation (2)

Re: BUG #4780: Aggregate functions are unaware of LIMITclauses in SELECTs

From: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
To: "Ted Holzman" <tholzman(at)fhcrc(dot)org>,<pgsql-bugs(at)postgresql(dot)org>
Subject: Re: BUG #4780: Aggregate functions are unaware of LIMITclauses in SELECTs
Date: 2009-04-24 16:35:22
Message-ID: 49F1A3FA.EE98.0025.0@wicourts.gov (view raw or flat)
Thread:
Lists: pgsql-bugs
"Ted Holzman" <tholzman(at)fhcrc(dot)org> wrote: 
 
> AGGREGATE functions don't appear to respond to LIMIT clauses.
 
Not a bug.  LIMIT affects how many rows are in the result set the
LIMIT qualifies.
 
> select sum(generate_series)
>   from generate_series(1,10) limit 3;
>  sum 
> -----
>   55
> (1 row)
> 
> I was expecting the sum to be 6.
 
The LIMIT is applied to the final result set, which is only one row,
so the LIMIT has nothing to do.  If you wanted to limit how many rows
went into the aggregate function, you'd need to do something like
this:
 
select sum(generate_series)
  from (select generate_series(1,10) limit 3) x;
 sum
-----
   6
(1 row)
 
-Kevin

In response to

pgsql-bugs by date

Next:From: Daniel GraceDate: 2009-04-25 00:03:54
Subject: Query with aggregate over subselects fails with ERROR: plan should not reference subplan's variable
Previous:From: Ted HolzmanDate: 2009-04-24 16:07:26
Subject: BUG #4780: Aggregate functions are unaware of LIMIT clauses in SELECTs

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group