Re: Combining two SELECTs

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Eric Jain" <jain(at)gmx(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Combining two SELECTs
Date: 2000-07-05 07:13:50
Message-ID: 823.962781230@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Eric Jain" <jain(at)gmx(dot)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.

(I'm assuming you need exactly these semantics, and not closely-
related ones as someone else suggested.)

By 7.2 or so, we hope to support sub-SELECTs in FROM, which'd let
you do this along the lines of

SELECT host,COUNT(*) FROM (SELECT DISTINCT host, ...)
GROUP BY ...

You might try to do it today by defining the SELECT DISTINCT as
a view and then selecting from the view with GROUP BY, but I
expect it won't work --- presently, views are implemented by
expanding the view macro-style, so they don't work for any case
that you couldn't write out as a single SQL-compliant query.
(Again, we hope to make this work better in 7.2.)

For now, the temp table seems like a good workaround.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Guillaume Perréal 2000-07-05 07:35:44 Re: Combining two SELECTs
Previous Message Philip Warner 2000-07-05 05:34:41 Re: [HACKERS] Re: Revised Copyright: is this morepalatable?