Re: group by/order by problem
Date: Mon, 24 May 2010 10:24:45 -0700 (PDT)
On May 23, 9:17 pm, Ken Quirici <kquir..._at_yahoo.com> wrote:
> On May 23, 11:54 am, "Vladimir M. Zakharychev"
> <vladimir.zakharyc..._at_gmail.com> wrote:
> > On May 23, 6:45 pm, Ken Quirici <kquir..._at_yahoo.com> wrote:
> > > Hi,
> > > The following query:
> > > select
> > > player,
> > > sum(hp) thp
> > > from b_data
> > > where hp = 1
> > > group by player
> > > order by thp
> > > ;
> > > does not sort the results either by player or thp.
> > > Is there some logical reason for this, or is order by only
> > > designed to sort group by's by the group by column?
> > > Basically, how can I get it to sort by thp?
> > > Any help MUCH appreciated.
> > > Regards,
> > > Ken Quirici
> > select player, thp
> > from (select player, sum(hp) thp
> > from b_data
> > where hp = 1
> > group by player)
> > order by thp;
> > To order by thp Oracle needs to "materialize" the inner view, so you
> > need to give it a chance to do so.
> > Hth,
> > Vladimir M. Zakharychev
> > N-Networks, makers of Dynamic PSP(tm)
> > http://www.dynamicpsp.com
> Hi Vladimir,
> I got the database restructured, and the revised code debugged.
> I tried out a query w/ a nested query like you suggested and it
> worked. Many thanks!
> However I'm not sure what you mean by 'materialize' the inner view.
> If there were no group by, there wouldn't be an issue - the order by
> would 'materialize' whatever query it was a clause of, whatever
> 'materialize' means. Why does the group by mean the order by
> can't to the same thing, if instead of a nested query, I simply
> append the order by clause to the group by query?
> I'm hope you understand how I don't understand, if you know
> what I mean.
> Again many thanks. It's enabled my project to lurch forward!
> Ken Quirici
My query was just a quick "patch" solution that would definitely work regardless. Not knowing exact details of your database, your data and your test case it was not possible to guess why it doesn't work as expected without asking quite a few additional questions (see below. :) ) I couldn't actually reproduce the issue as presented in your post in a 10.1.0.4 (yes, that outdated,) database. Results are correctly sorted as in Serge's example in both directions. What's your Oracle version? What's the DDL and sample data? What was the plan for the query and what was your output? In my test case (where it worked correctly) the plan was:
SORT ORDER BY
SORT GROUP BY
TABLE ACCESS FULL Oracle automatically materialized the group by step before sorting it. By "materializing" I mean that to perform the ORDER BY sort, Oracle needs to fetch all rows to be sorted first and store them somewhere. That's what we forced it to do by utilizing the inner view.
Anyway, if you can easily reproduce the issue, it looks like a bug worth opening a SR with "wrong results from a query" tag.
Vladimir M. Zakharychev
N-Networks, makers of Dynamic PSP(tm) http://www.dynamicpsp.com Received on Mon May 24 2010 - 12:24:45 CDT