Re: order by and aggregate - Mailing list pgsql-sql

From Tom Lane
Subject Re: order by and aggregate
Date
Msg-id 14734.1041870875@sss.pgh.pa.us
Whole thread Raw
In response to order by and aggregate  (Tomasz Myrta <jasiek@klaster.net>)
List pgsql-sql
Tomasz Myrta <jasiek@klaster.net> writes:
> I found I can obey this using subselect:
> select
>    maxsum(X.value)
> from
>   (select value
>    from some_table
>    order by some_field) X

> I can't create subselect, because I want to change this query into a 
> view. In my case postgresql doesn't use indexes on subselect inside a view.

I don't understand what you think is wrong with this solution.  It works
fine for me:

regression=# create view vv as
regression-# select max(unique1) from (select unique1 from tenk1
regression(# order by unique2) x;
CREATE VIEW
regression=# explain select * from vv;                                           QUERY PLAN

---------------------------------------------------------------------------------------------------Subquery Scan vv
(cost=523.00..523.00rows=1 width=8)  ->  Aggregate  (cost=523.00..523.00 rows=1 width=8)        ->  Subquery Scan x
(cost=0.00..498.00rows=10000 width=8)              ->  Index Scan using tenk1_unique2 on tenk1  (cost=0.00..498.00
rows=10000width=8)
 
(4 rows)
        regards, tom lane


pgsql-sql by date:

Previous
From: Tomasz Myrta
Date:
Subject: Re: order by and aggregate
Next
From: Tom Lane
Date:
Subject: Re: order by and aggregate