how to write an optimized sql with two same subsql? - Mailing list pgsql-general

From sunpeng
Subject how to write an optimized sql with two same subsql?
Date
Msg-id AANLkTin6fUFO3mO-AMEX=-xWQV16gOzmyWcRdBWL+SSw@mail.gmail.com
Whole thread Raw
Responses Re: how to write an optimized sql with two same subsql?  (Rob Sargent <robjsargent@gmail.com>)
Re: how to write an optimized sql with two same subsql?  ("Igor Neyman" <ineyman@perceptron.com>)
List pgsql-general
We have a table A:
CREATE TABLE A(
   uid integer,
   groupid integer
)
Now we use this subsql to get each group's count:
SELECT count(*) as count
FROM A
GROUP BY groupid
ORDER BY groupid

Then we try to find the group pair with following conditions:
SELECT c.groupid as groupid1,d.groupid as groupid2
FROM subsql as c, subsql as d
WHERE d.groupid > c.groupid
              and d.count > c.count;

Does that mean subsql will be executed twice? or how to write the optimized sql?


pgsql-general by date:

Previous
From: John R Pierce
Date:
Subject: Re: how to get current sql execution time?
Next
From: Rob Sargent
Date:
Subject: Re: how to write an optimized sql with two same subsql?