On 11/19/2010 03:21 AM, Thom Brown wrote:
>
> You should always use ORDER BY on the outer-most part of the query
> since that's what will be finally returning your data. Don't bother
> with ordering sub-selects.
I definiatelly have to use the "order by" inside for two reasons.
When "distinct on (x)" is used then x must be in the first column in the
order by part.
The second column in the order by decides which records will I include
in the join so it is very important to use it for ordering.
> So in your case, just use:
>
> SELECT *
> FROM (SELECT DISTINCT ON (b_id) * FROM a) sub
> LEFT JOIN b ON b.id = sub.b_id
> ORDER BY sub.b_id, sub.id;
select distinct on (id) * from b order by name;
ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY
expressions
> But why bother with a sub-select anyway? You can write it as:
>
> SELECT DISTINCT ON (a.b_id) *
> FROM a
> LEFT JOIN b ON b.id = a.b_id
> ORDER BY a.b_id, a.id;
I considered this, however the subquery is generated by an ORM. I wanted
to separate it.
Also the whole join affects many rows. I thought it's cheaper to
preselect them inside the subquery then do the join. I am not sure.
Explain analyze is my good friend but in this case I prefer to ask.
Mage