Обсуждение: WHERE of an AGGREGATE ...

Поиск
Список
Период
Сортировка

WHERE of an AGGREGATE ...

От
The Hermit Hacker
Дата:
I have a query that looks like:
 SELECT SUM(bytes) AS traffic   FROM traffic_table
GROUP BY ip;

I want to narrow that SELECT down to a subset, like:
  SELECT SUM(bytes) AS traffic    FROM traffic_table   WHERE traffic < ( 100 * 1024 * 1024 )GROUP BY ip;

which, of course, won't work, cause I need to do the GROUP BY before I do
the WHERE ... but there has to be a way of coding that so that it does
work :(

I'm overlooking something obvious here ... I think?

Thanks ...

Marc G. Fournier                   ICQ#7615664               IRC Nick: Scrappy
Systems Administrator @ hub.org
primary: scrappy@hub.org           secondary: scrappy@{freebsd|postgresql}.org


Re: WHERE of an AGGREGATE ...

От
deststar
Дата:
Isn't that what the having clause is about?
- Stuart

On Sun, 2003-06-15 at 00:31, The Hermit Hacker wrote:
> I have a query that looks like:
> 
>   SELECT SUM(bytes) AS traffic
>     FROM traffic_table
> GROUP BY ip;
> 
> I want to narrow that SELECT down to a subset, like:
> 
>    SELECT SUM(bytes) AS traffic
>      FROM traffic_table
>     WHERE traffic < ( 100 * 1024 * 1024 )
>  GROUP BY ip;
> 
> which, of course, won't work, cause I need to do the GROUP BY before I do
> the WHERE ... but there has to be a way of coding that so that it does
> work :(
> 
> I'm overlooking something obvious here ... I think?
> 
> Thanks ...
> 
> Marc G. Fournier                   ICQ#7615664               IRC Nick: Scrappy
> Systems Administrator @ hub.org
> primary: scrappy@hub.org           secondary: scrappy@{freebsd|postgresql}.org
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 9: the planner will ignore your desire to choose an index scan if your
>       joining column's datatypes do not match



Re: WHERE of an AGGREGATE ...

От
The Hermit Hacker
Дата:
perfect, its one of those that I've used so rarely, I never thought about
it ... thanks :)


On Sat, 15 Jun 2003, deststar wrote:

> Isn't that what the having clause is about?
> - Stuart
>
> On Sun, 2003-06-15 at 00:31, The Hermit Hacker wrote:
> > I have a query that looks like:
> >
> >   SELECT SUM(bytes) AS traffic
> >     FROM traffic_table
> > GROUP BY ip;
> >
> > I want to narrow that SELECT down to a subset, like:
> >
> >    SELECT SUM(bytes) AS traffic
> >      FROM traffic_table
> >     WHERE traffic < ( 100 * 1024 * 1024 )
> >  GROUP BY ip;
> >
> > which, of course, won't work, cause I need to do the GROUP BY before I do
> > the WHERE ... but there has to be a way of coding that so that it does
> > work :(
> >
> > I'm overlooking something obvious here ... I think?
> >
> > Thanks ...
> >
> > Marc G. Fournier                   ICQ#7615664               IRC Nick: Scrappy
> > Systems Administrator @ hub.org
> > primary: scrappy@hub.org           secondary: scrappy@{freebsd|postgresql}.org
> >
> > ---------------------------(end of broadcast)---------------------------
> > TIP 9: the planner will ignore your desire to choose an index scan if your
> >       joining column's datatypes do not match
>
>

Marc G. Fournier                   ICQ#7615664               IRC Nick: Scrappy
Systems Administrator @ hub.org
primary: scrappy@hub.org           secondary: scrappy@{freebsd|postgresql}.org