pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
postgres sql help
4+ messages / 4 participants
[nested] [flat]

* postgres sql help
@ 2011-10-14 15:41 James Bond <coolofall@hotmail.com>
  2011-10-16 08:11 ` Re: postgres sql help Andreas Kretschmer <akretschmer@spamfence.net>
  2011-10-16 08:24 ` Re: postgres sql help Harald Fuchs <hari.fuchs@gmail.com>
  0 siblings, 2 replies; 4+ messages in thread

From: James Bond @ 2011-10-14 15:41 UTC (permalink / raw)
  To: pgsql-sql


hi, i am fairly new in postgresql, so if anyone can help me would be great
 

if i simply do:
 

select ver_no

from version

order by ver_no
 

the result will be something like this:
 

.1.3.1

.1.3.2.5.

.1.4.1.7.12

.1.4.11.14.7.

.1.4.3.109.1.

.1.4.8.66.
 

so as you can see first 3 lines are ok, but how to make 1.4.3.109 come 
before 1.4.11 because the third level "3" is smaller than "11".  i 
understand they are stored as char so i used split_part function to 
separate each numbers between "." in a separate column. but when i try 
to convert those column into integer, i am getting an error msg saying: 
ERROR:  invalid input syntax for type numeric: " "
 

here is my code:
 

select ver_no, duedate, status,

to_number(split_part(ver_no, '.', 2), '9999') a,

to_number(split_part(ver_no, '.', 3), '9999') b,

to_number(split_part(ver_no, '.', 4), '9999') c,

to_number(split_part(ver_no, '.', 5), '9999') d
 
 

from version
 

order by a,b,c,d
 

I am not sure if i am heading towards wrong direction but can someone please suggest or give me some other ideas to sort this.  
 

PS: i found some good solution in SQL Server but those commands are not used in postgreSQL.

Thanks in advance

- James

 		 	   		  =

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: postgres sql help
  2011-10-14 15:41 postgres sql help James Bond <coolofall@hotmail.com>
@ 2011-10-16 08:11 ` Andreas Kretschmer <akretschmer@spamfence.net>
  1 sibling, 0 replies; 4+ messages in thread

From: Andreas Kretschmer @ 2011-10-16 08:11 UTC (permalink / raw)
  To: pgsql-sql

James Bond <coolofall@hotmail.com> wrote:

> hi, i am fairly new in postgresql, so if anyone can help me would be great
> 
> if i simply do:
> 
> select ver_no
> from version
> order by ver_no
> 
> the result will be something like this:
> 
> .1.3.1
> .1.3.2.5.
> .1.4.1.7.12
> .1.4.11.14.7.
> .1.4.3.109.1.
> .1.4.8.66.
> 
> so as you can see first 3 lines are ok, but how to make 1.4.3.109 come before
> 1.4.11 because the third level "3" is smaller than "11". i understand they are
> stored as char so i used split_part function to separate each numbers between
> "." in a separate column. but when i try to convert those column into integer,
> i am getting an error msg saying: ERROR: invalid input syntax for type numeric:
> " "
> 
> here is my code:

test=*# select * from o;
       t
---------------
 .1.3.1
 .1.3.2.5.
 .1.4.1.7.12
 .1.4.11.14.7.
 .1.4.3.109.1.
 .1.4.8.66.
(6 rows)

Time: 0,262 ms
test=*# select * from o order by split_part(t,'.',2)::int,
split_part(t,'.',3)::int, split_part(t,'.',4)::int;
       t
---------------
 .1.3.1
 .1.3.2.5.
 .1.4.1.7.12
 .1.4.3.109.1.
 .1.4.8.66.
 .1.4.11.14.7.
(6 rows)

Time: 0,403 ms


Andreas
-- 
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect.                              (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly."   (unknown)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: postgres sql help
  2011-10-14 15:41 postgres sql help James Bond <coolofall@hotmail.com>
@ 2011-10-16 08:24 ` Harald Fuchs <hari.fuchs@gmail.com>
  2011-10-17 20:44   ` Re: postgres sql help Tim Landscheidt <tim@tim-landscheidt.de>
  1 sibling, 1 reply; 4+ messages in thread

From: Harald Fuchs @ 2011-10-16 08:24 UTC (permalink / raw)
  To: pgsql-sql

In article <SNT102-W49402A41C6B6599F082D35A8E10@phx.gbl>,
James Bond <coolofall@hotmail.com> writes:

> hi, i am fairly new in postgresql, so if anyone can help me would be great
> if i simply do:

> select ver_no
> from version
> order by ver_no

> the result will be something like this:

> .1.3.1
> .1.3.2.5.
> .1.4.1.7.12
> .1.4.11.14.7.
> .1.4.3.109.1.
> .1.4.8.66.

> so as you can see first 3 lines are ok, but how to make 1.4.3.109 come
> before 1.4.11 because the third level "3" is smaller than "11".

The query

  SELECT ver_no
  FROM version
  ORDER BY string_to_array(ver_no, '.', '')::int[]

should do what you want.




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: postgres sql help
  2011-10-14 15:41 postgres sql help James Bond <coolofall@hotmail.com>
  2011-10-16 08:24 ` Re: postgres sql help Harald Fuchs <hari.fuchs@gmail.com>
@ 2011-10-17 20:44   ` Tim Landscheidt <tim@tim-landscheidt.de>
  0 siblings, 0 replies; 4+ messages in thread

From: Tim Landscheidt @ 2011-10-17 20:44 UTC (permalink / raw)
  To: pgsql-sql

Harald Fuchs <hari.fuchs@gmail.com> wrote:

>> hi, i am fairly new in postgresql, so if anyone can help me would be great
>> if i simply do:

>> select ver_no
>> from version
>> order by ver_no

>> the result will be something like this:

>> .1.3.1
>> .1.3.2.5.
>> .1.4.1.7.12
>> .1.4.11.14.7.
>> .1.4.3.109.1.
>> .1.4.8.66.

>> so as you can see first 3 lines are ok, but how to make 1.4.3.109 come
>> before 1.4.11 because the third level "3" is smaller than "11".

> The query

>   SELECT ver_no
>   FROM version
>   ORDER BY string_to_array(ver_no, '.', '')::int[]

> should do what you want.

Really neat! :-) For pre-9.1, you have to "ORDER BY
string_to_array(TRIM('.' FROM ver_no), '.')::int[];",
though.

Tim




^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2011-10-17 20:44 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-10-14 15:41 postgres sql help James Bond <coolofall@hotmail.com>
2011-10-16 08:11 ` Andreas Kretschmer <akretschmer@spamfence.net>
2011-10-16 08:24 ` Harald Fuchs <hari.fuchs@gmail.com>
2011-10-17 20:44   ` Tim Landscheidt <tim@tim-landscheidt.de>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox