Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question.

Asked: March 22, 2017 - 2:55 pm UTC

Last updated: March 23, 2017 - 2:14 pm UTC

Version: 11.2.0.4

Viewed 1000+ times

You Asked

Hello,

select rn, code, max(code) over () as mcode from
(
select 1 as rn, '1C538454-D89A-E211-88D1-1CC1DEE781E4' as code from dual
    union all
select 2 as rn, 'C5FAEF8F-D1B0-E211-A69F-1CC1DEE781E4' as code from dual
)
order by code asc

result:
RN CODE MCODE
2 C5FAEF8F-D1B0-E211-A69F-1CC1DEE781E4 C5FAEF8F-D1B0-E211-A69F-1CC1DEE781E4
1 1C538454-D89A-E211-88D1-1CC1DEE781E4 C5FAEF8F-D1B0-E211-A69F-1CC1DEE781E4


Why does ORDER BY ASC get first with MAX value of code?

Thanks!

and Chris said...

That's not the behaviour I see when using 11.2.0.4, the results are ordered by code ascending as you'd expect:

SQL> select * from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
CORE    11.2.0.4.0      Production
TNS for Linux: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production

SQL>
SQL> select rn, code, max(code) over () as mcode from
  2  (
  3  select 1 as rn, '1C538454-D89A-E211-88D1-1CC1DEE781E4' as code from dual
  4      union all
  5  select 2 as rn, 'C5FAEF8F-D1B0-E211-A69F-1CC1DEE781E4' as code from dual
  6  )
  7  order by code asc;

        RN CODE                                 MCODE
---------- ------------------------------------ ------------------------------------
         1 1C538454-D89A-E211-88D1-1CC1DEE781E4 C5FAEF8F-D1B0-E211-A69F-1CC1DEE781E4
         2 C5FAEF8F-D1B0-E211-A69F-1CC1DEE781E4 C5FAEF8F-D1B0-E211-A69F-1CC1DEE781E4


So:

- what version exactly are you using?
- What are you using to view the results?
- Is it possible your client is sorting the data after you fetch it?

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

I've got it: it depends on nls_sort

Serj, March 23, 2017 - 11:52 am UTC

Thanks for your response.
I've got it: it depends on nls_sort. I used russian nls parameters by default. But I seems strange max and order use different settings.

Chris Saxon
March 23, 2017 - 2:14 pm UTC

I thought there must be something else going on!

If you share your test case (including your NLS settings) we can look further

More to Explore

Analytics

Analytic SQL got you confused? Check out Connor McDonald's complete video course.