Гость
Целевая тема:
Создать новую тему:
Автор:
Форумы / PostgreSQL [игнор отключен] [закрыт для гостей] / Order by по полю со значениями 1,1а,1b,2,2a,3 и так далее.. / 8 сообщений из 8, страница 1 из 1
03.04.2016, 16:23
    #39206881
Novichek_nes
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Order by по полю со значениями 1,1а,1b,2,2a,3 и так далее..
Добрый день!
Помогите,пожалуйста, новичку :

Есть поле c именем "noname" [varchar(5)] в таблицe "name" со значениями содержащими цифру и букву -
Запрос select noname from name order by noname ;
Дает такой результат:


"1"
"10"
"10a"
"10c"
"10d"
"11"
"11a"
"11b"
"12"
"13"
"14"
"14b"
"14c"
"14d"
"15"
"16"
"17"
"18"
"19"
"1a"
"1b"
"1c"
"2"
"20"
"20a"
"20b"
"21"
"21a"
"21b"
"21c"
"21d"
"21e"
"22"
"23"
"23b"
"23c"
"24"
"24b"
"25"
"26"
"27"
"27a"
"3"
"30"
"31"
"32"
"3a"
"4"
"40a"
"40b"
"41"
"41b"
"41c"
"42a"
"43"
"43b"
"43c"
"43d"
"44"
"45"
"45a"
"45b"
"45c"
"45d"
"4b"
"4c"
"4d"
"4e"
"5"
"5a"
"6"


Как сделать order by так , чтобы сортировка была сначала по цифре потом по букве , то есть -

1
1a
1b
1c
2
3
3a
4
4b
4c
4d
и так далее ..



Спасибо!
...
Рейтинг: 0 / 0
03.04.2016, 16:53
    #39206893
Щукина Анна
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Order by по полю со значениями 1,1а,1b,2,2a,3 и так далее..
Novichek_nes,

сортируйте по полю, дополненнему слева пробелами до нужной длины (lpad())...
...
Рейтинг: 0 / 0
03.04.2016, 17:29
    #39206915
Novichek_nes
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Order by по полю со значениями 1,1а,1b,2,2a,3 и так далее..
Щукина Анна,


Анна , спасибо!
Можете в двух словах ,пожалуйста, как поможет добавление пробелов слева к полю?
Если можно в виде примера простенького ?

Спасибо!!
...
Рейтинг: 0 / 0
03.04.2016, 18:13
    #39206932
Alexander A. Sak
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Order by по полю со значениями 1,1а,1b,2,2a,3 и так далее..
Перед дополнением слева надо убрать нечисловые символы. Как-то так:

Код: plsql
1.
2.
select lpad(regexp_replace(noname, '\D*$', ''), 5, '0')
from name;



Можно и без регэкспов, но будет выглядеть более громоздко.
...
Рейтинг: 0 / 0
03.04.2016, 19:33
    #39206953
Щукина Анна
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Order by по полю со значениями 1,1а,1b,2,2a,3 и так далее..
Novichek_nes,
<=== или такой вариант:
Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
--
-- Тестовые данные:
with
  t as 
    (
      select '1' as str union all
      select '10' as str union all
      select '10a' as str union all
      select '10c' as str union all
      select '10d' as str union all
      select '11' as str union all
      select '11a' as str union all
      select '11b' as str union all
      select '12' as str union all
      select '13' as str union all
      select '14' as str union all
      select '14b' as str union all
      select '14c' as str union all
      select '14d' as str union all
      select '15' as str union all
      select '16' as str union all
      select '17' as str union all
      select '18' as str union all
      select '19' as str union all
      select '1a' as str union all
      select '1b' as str union all
      select '1c' as str union all
      select '2' as str union all
      select '20' as str union all
      select '20a' as str union all
      select '20b' as str union all
      select '21' as str union all
      select '21a' as str union all
      select '21b' as str union all
      select '21c' as str union all
      select '21d' as str union all
      select '21e' as str union all
      select '22' as str union all
      select '23' as str union all
      select '23b' as str union all
      select '23c' as str union all
      select '24' as str union all
      select '24b' as str union all
      select '25' as str union all
      select '26' as str union all
      select '27' as str union all
      select '27a' as str union all
      select '3' as str union all
      select '30' as str union all
      select '31' as str union all
      select '32' as str union all
      select '3a' as str union all
      select '4' as str union all
      select '40a' as str union all
      select '40b' as str union all
      select '41' as str union all
      select '41b' as str union all
      select '41c' as str union all
      select '42a' as str union all
      select '43' as str union all
      select '43b' as str union all
      select '43c' as str union all
      select '43d' as str union all
      select '44' as str union all
      select '45' as str union all
      select '45a' as str union all
      select '45b' as str union all
      select '45c' as str union all
      select '45d' as str union all
      select '4b' as str union all
      select '4c' as str union all
      select '4d' as str union all
      select '4e' as str union all
      select '5' as str union all
      select '5a' as str union all
      select '6' as str
    )
--
-- Основной запрос:
select str
  from t
 order by rtrim(str,ltrim(str,'1234567890'))::integer
        , ltrim(str,'1234567890')
 

on-line проверка на sqlfiddle.com
...
Рейтинг: 0 / 0
03.04.2016, 19:39
    #39206955
Щукина Анна
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Order by по полю со значениями 1,1а,1b,2,2a,3 и так далее..
Щукина АннаNovichek_nes,
<=== или такой вариант:
Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
--
-- Тестовые данные:
with
  t as 
    (
      select '1' as str union all
      select '10' as str union all
      select '10a' as str union all
      select '10c' as str union all
      select '10d' as str union all
      select '11' as str union all
      select '11a' as str union all
      select '11b' as str union all
      select '12' as str union all
      select '13' as str union all
      select '14' as str union all
      select '14b' as str union all
      select '14c' as str union all
      select '14d' as str union all
      select '15' as str union all
      select '16' as str union all
      select '17' as str union all
      select '18' as str union all
      select '19' as str union all
      select '1a' as str union all
      select '1b' as str union all
      select '1c' as str union all
      select '2' as str union all
      select '20' as str union all
      select '20a' as str union all
      select '20b' as str union all
      select '21' as str union all
      select '21a' as str union all
      select '21b' as str union all
      select '21c' as str union all
      select '21d' as str union all
      select '21e' as str union all
      select '22' as str union all
      select '23' as str union all
      select '23b' as str union all
      select '23c' as str union all
      select '24' as str union all
      select '24b' as str union all
      select '25' as str union all
      select '26' as str union all
      select '27' as str union all
      select '27a' as str union all
      select '3' as str union all
      select '30' as str union all
      select '31' as str union all
      select '32' as str union all
      select '3a' as str union all
      select '4' as str union all
      select '40a' as str union all
      select '40b' as str union all
      select '41' as str union all
      select '41b' as str union all
      select '41c' as str union all
      select '42a' as str union all
      select '43' as str union all
      select '43b' as str union all
      select '43c' as str union all
      select '43d' as str union all
      select '44' as str union all
      select '45' as str union all
      select '45a' as str union all
      select '45b' as str union all
      select '45c' as str union all
      select '45d' as str union all
      select '4b' as str union all
      select '4c' as str union all
      select '4d' as str union all
      select '4e' as str union all
      select '5' as str union all
      select '5a' as str union all
      select '6' as str
    )
--
-- Основной запрос:
select str
  from t
 order by rtrim(str,ltrim(str,'1234567890'))::integer
        , ltrim(str,'1234567890')
 

on-line проверка на sqlfiddle.com
Хотя, для сортировки по буквенной части можно использовать и саму исходную строку, без преобразований:
Код: sql
1.
2.
3.
4.
5.
-- Основной запрос:
select str
  from t
 order by rtrim(str,ltrim(str,'1234567890'))::integer
        , str
...
Рейтинг: 0 / 0
04.04.2016, 08:48
    #39207121
Novichek_nes
Гость
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Order by по полю со значениями 1,1а,1b,2,2a,3 и так далее..
Ребята, спасибо большое!
Предложенные вами примеры привели меня к селекту типа -

select noname
from name
ORDER BY
regexp_replace(noname, '\D', '')::integer,
regexp_replace(noname, '\d', '');

Который выдает нужный мне дата сет :

1
1a
1b
1c
2
3
3a
4
4b
4c
4d
4e
5
5a
6
7
7a
8
8a
8b
8c
8e
9
10
10a
10c
10d
11
11a
11b
12
13
14
14b
14c
14d
15
16
17
18
19
20
20a
20b
21
21a
21b
21c
21d
21e
22
23
23b
23c
24
24b
25
26
27
27a
30
31
32
40a
40b
41
41b
41c
42a
43

И так далее ...
...
Рейтинг: 0 / 0
04.04.2016, 12:04
    #39207360
bochkov
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Order by по полю со значениями 1,1а,1b,2,2a,3 и так далее..
Novichek_nes,
очень похоже на HEX
Код: sql
1.
ORDER BY DECODE(LPAD(noname,16,'0'),'HEX')
...
Рейтинг: 0 / 0
Форумы / PostgreSQL [игнор отключен] [закрыт для гостей] / Order by по полю со значениями 1,1а,1b,2,2a,3 и так далее.. / 8 сообщений из 8, страница 1 из 1
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]