Re: Combining two SELECTs - Mailing list pgsql-general

From Guillaume Perréal
Subject Re: Combining two SELECTs
Date
Msg-id 3962E550.B685F556@lyon.cemagref.fr
Whole thread Raw
In response to Combining two SELECTs  ("Eric Jain" <jain@gmx.net>)
List pgsql-general
Tom Lane wrote:
>
> "Eric Jain" <jain@gmx.net> writes:
> > Any ideas how the following two statements could be combined into a
> > single one?
>
> > SELECT DISTINCT host, url, id
> > INTO TEMP
> > FROM log
> > WHERE
> >   host IN (SELECT host FROM robots)
> >   AND status IN (200, 304);
>
> > SELECT host, COUNT(*) AS hits
> > FROM TEMP
> > GROUP BY host
> > ORDER BY hits DESC;
>
> Offhand I do not think you can do this in one "simple" SQL query,
> because the SQL query semantics require that GROUP BY grouping occurs
> before DISTINCT processing, whereas you want the other order.
>
>
> For now, the temp table seems like a good workaround.
>

And splitting some complex queries in simpler ones (using temp tables) can
increase performance, depending on the query.

Regards,
Guillaume Perréal - Stagiaire MIAG
Cemagref (URH), Lyon, France
Tél: (+33) 4.72.20.87.64

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Combining two SELECTs
Next
From: Gilles DAROLD
Date:
Subject: Re: responses to licensing discussion