Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old November 22nd, 2005, 08:52 AM
Steve Crawford
Guest
 
Posts: n/a
Default Sort order confusion

I am suffering some sort order confusion. Given a database, "foo",
with a single character(4) column of data left padded with spaces I
get:

select * from foo order by somechars;

somechars
-----------
0
1
10
100
1011
111
1512
2
222
3
333

The output I desire is a basic ASCII sort:

somechars
-----------
0
1
2
3
10
100
111
222
333
1011
1512

(This sample set just has spaces and numeric digits but could have
other characters - I want the output in ASCII sort order)

1. What is the correct way to do this?

2. How do I verify the locale setting of an existing database cluster
(to verify that I really initialized it as "initdb -d --locale=C -D
/var/lib/pgsql/data")?

3. Should I have included "--enable-locale" or similar option when I
built Postgresql (the build is vanilla 7.4.1 "./configure ; make ;
make install")?

Cheers,
Steve


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo@postgresql.org)




  #2  
Old November 22nd, 2005, 08:52 AM
Stephen Robert Norris
Guest
 
Posts: n/a
Default Re: Sort order confusion

On Fri, 2004-02-06 at 11:12, Steve Crawford wrote:[color=blue]
> I am suffering some sort order confusion. Given a database, "foo",
> with a single character(4) column of data left padded with spaces I
> get:
>
> select * from foo order by somechars;
>
> somechars
> -----------
> 0
> 1
> 10
> 100
> 1011
> 111
> 1512
> 2
> 222
> 3
> 333
>
> The output I desire is a basic ASCII sort:
>
> somechars
> -----------
> 0
> 1
> 2
> 3
> 10
> 100
> 111
> 222
> 333
> 1011
> 1512
>
> (This sample set just has spaces and numeric digits but could have
> other characters - I want the output in ASCII sort order)[/color]

Your original sort is a basic lexigraphic ("alphabetical" by ASCII
character set number) sort.

What you appear to want is a numeric sort, where the numbers come out in
the order of numbers, rather than in their ASCII character set order.

If there were just digits that would be fairly easy, but I can't see any
way (short of post-processing the list in other software or writing
yourself a stored procedure) to do it in SQL.

Stephen

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQBAIt4V4hFS2REFUecRAkPYAJ0dUnhZDthau4u3rlY2u/Zu6ndbMQCginci
zu1Wv3XR0KtNK85NkonbciY=
=VAoe
-----END PGP SIGNATURE-----

  #3  
Old November 22nd, 2005, 08:52 AM
Steve Crawford
Guest
 
Posts: n/a
Default Re: Sort order confusion

On Thursday 05 February 2004 4:21 pm, Stephen Robert Norris wrote:[color=blue]
> On Fri, 2004-02-06 at 11:12, Steve Crawford wrote:[color=green]
> > I am suffering some sort order confusion. Given a database,
> > "foo", with a single character(4) column of data left padded with
> > spaces I get:
> >
> > select * from foo order by somechars;
> >
> > somechars
> > -----------
> > 0
> > 1
> > 10
> > 100
> > 1011
> > 111
> > 1512
> > 2
> > 222
> > 3
> > 333[/color][/color]
<snip>

[color=blue]
> Your original sort is a basic lexigraphic ("alphabetical" by ASCII
> character set number) sort.
>
> What you appear to want is a numeric sort, where the numbers come
> out in the order of numbers, rather than in their ASCII character
> set order.[/color]

Not exactly. I _DO_ want it in ASCII character set order which
includes spaces (0x20) sorting ahead of digits (0x30 - 0x39). This is
not what is happening. The first sort is some SQL sort order that
seems to ignore certain characters. Note the different sort order if
I pad with 'x' instead of '<space>':

somechars
-----------
1011
1512
x100
x111
x222
x333
x444
x555
x666
x777
x888
xx10
xx44
xx55
xxx0
xxx1
xxx2
xxx3
xxx4
xxx5
xxx6
xxx7
xxx8
xxx9
xxxx

Naturally if I were dealing with fields guaranteed to have something
that would convert to an int I could just order by, say,
int4(somechars) but that is not the case.

I even tried the to_ascii function but apparently that's the wrong
approach:
ERROR: encoding conversion from SQL_ASCII to ASCII not supported

Cheers,
Steve


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

  #4  
Old November 22nd, 2005, 08:52 AM
Alvaro Herrera
Guest
 
Posts: n/a
Default Re: Sort order confusion

On Fri, Feb 06, 2004 at 11:21:41AM +1100, Stephen Robert Norris wrote:[color=blue]
> On Fri, 2004-02-06 at 11:12, Steve Crawford wrote:[color=green]
> > I am suffering some sort order confusion. Given a database, "foo",
> > with a single character(4) column of data left padded with spaces I
> > get:
> >
> > select * from foo order by somechars;[/color][/color]

Cast it:

alvherre=> select * from foo order by bar::text::int;
bar
------
0
1
10
33
100
101
333
503
(8 filas)

--
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
"La naturaleza, tan frágil, tan expuesta a la muerte... y tan viva"

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

  #5  
Old November 22nd, 2005, 08:52 AM
Steve Crawford
Guest
 
Posts: n/a
Default Re: Sort order confusion

On Thursday 05 February 2004 5:01 pm, Alvaro Herrera wrote:[color=blue]
> On Fri, Feb 06, 2004 at 11:21:41AM +1100, Stephen Robert Norris[/color]
wrote:[color=blue][color=green]
> > On Fri, 2004-02-06 at 11:12, Steve Crawford wrote:[color=darkred]
> > > I am suffering some sort order confusion. Given a database,
> > > "foo", with a single character(4) column of data left padded
> > > with spaces I get:
> > >
> > > select * from foo order by somechars;[/color][/color]
>
> Cast it:
>
> alvherre=> select * from foo order by bar::text::int;
> bar
> ------
> 0
> 1
> 10
> 33
> 100
> 101
> 333
> 503
> (8 filas)[/color]

Can't. As noted in the original post the column may contain data that
won't convert to an int (all spaces, characters, punctuation). I'm
seeking generic true ASCII sort order.

Cheers,
Steve


---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

  #6  
Old November 22nd, 2005, 08:52 AM
Stephan Szabo
Guest
 
Posts: n/a
Default Re: Sort order confusion

On Thu, 5 Feb 2004, Steve Crawford wrote:
[color=blue]
> 2. How do I verify the locale setting of an existing database cluster
> (to verify that I really initialized it as "initdb -d --locale=C -D
> /var/lib/pgsql/data")?[/color]

I think it'd be
pg_controldata /var/lib/pgsql/data


---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

  #7  
Old November 22nd, 2005, 08:52 AM
Tom Lane
Guest
 
Posts: n/a
Default Re: Sort order confusion

Steve Crawford <scrawford@pinpointresearch.com> writes:[color=blue]
> Not exactly. I _DO_ want it in ASCII character set order which
> includes spaces (0x20) sorting ahead of digits (0x30 - 0x39). This is
> not what is happening. The first sort is some SQL sort order that
> seems to ignore certain characters.[/color]

Sounds to me like you've got the database in a non-C locale. See past
discussions ...

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

  #8  
Old November 22nd, 2005, 08:55 AM
Steve Crawford
Guest
 
Posts: n/a
Default Re: Sort order confusion

On Thursday 05 February 2004 6:08 pm, Tom Lane wrote:[color=blue]
> Steve Crawford <scrawford@pinpointresearch.com> writes:[color=green]
> > Not exactly. I _DO_ want it in ASCII character set order which
> > includes spaces (0x20) sorting ahead of digits (0x30 - 0x39).
> > This is not what is happening. The first sort is some SQL sort
> > order that seems to ignore certain characters.[/color]
>
> Sounds to me like you've got the database in a non-C locale. See
> past discussions ...[/color]

That was my first inclination (as noted in the full version of my
original post - now lost to the thread) but my installation history
showed:
initdb -d --locale=C -D /var/lib/pgsql/data

Nonetheless pg_controldata shows:
LC_COLLATE: en_US
LC_CTYPE: en_US

I now suspect that currently active "real" database was in-fact
created not by my several test initializations but by the SuSE
startup script which will run an "initdb" if the database has not
been initialized and which reads /etc/sysconfig/language to get
default locale settings.

So...

Is there a method of changing the locale of an extant database or do I
need to dump ; delete db ; reinit ; restore ?

Cheers,
Steve


---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,414 network members.