如何在oracle 10g中根据居住地址获得排序结果?

发布于 2024-11-28 15:53:56 字数 301 浏览 2 评论 0原文

我想查询Oracle 10g数据库并根据居住地址按升序或降序排列得到结果。所以问题是构建地址有时是110C或200D的形式,所以根据sql“order by”asc或desc,我得到的结果是20之前的200D。 例如,如果地址是 10 110 112 200D 232 95 20 100A 1050 944

按升序排列,它将导致:-

 10 100 100A 1050 110 112 20 200D 232 944 95

问题是由于地址中包含字符,我不能将它们视为整数或数字,它们必须被视为字符串。

I want to query the Oracle 10g database and get result on the basis of residential address in ascending or descending order. So the problem is building addresses are at time in the form of 110C or 200D, so according to sql "order by" asc or desc, I get the result as 200D before 20.
for eg, if adresses are 10 110 112 200D 232 95 20 100A 1050 944

In ascending order it will result in:-

 10 100 100A 1050 110 112 20 200D 232 944 95

The problem is as the adresses have characters in it, i can't consider them to be as integers or number, they have to considered as String.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

〗斷ホ乔殘χμё〖 2024-12-05 15:53:56

使用正则表达式:

警告!未来潜在的非工作代码。我目前没有 Oracle 实例来测试它。

SELECT YourAddress
FROM YourTable
order by TO_NUMBER(REGEXP_SUBSTR(AddressColumn, '^[0-9]+'));

REGEXP_SUBSTR 将找到应该开始地址的数字子字符串,将其转换为实数并按其排序。

警告:您必须改进查询以处理数字不存在的情况。然而,这个答案可以让你很好地开始。

当需要微调查询时,您应该使用以下资源:

而且,是的,REGEXP_SUBSTR 在 Oracle 10g 中可用。

Use regular expressions:

Warning! Potential non-working code ahead. I do not have an Oracle instance to test it against at the moment.

SELECT YourAddress
FROM YourTable
order by TO_NUMBER(REGEXP_SUBSTR(AddressColumn, '^[0-9]+'));

REGEXP_SUBSTR will find the number's substring which is supposed to start the address, convert it to a real number and order by it.

Caveat: you will have to improve the query to handle cases where a number will not be present. However, this answer can get you very well started.

When in need to fine-tune the query, here are the resources you should use:

And, yes, REGEXP_SUBSTR is available in Oracle 10g.

往日 2024-12-05 15:53:56

基于正则表达式的解决方案更加优雅。但假设您希望首先使用数字排序对数字部分进行排序,然后使用字符排序对字符部分进行排序,则还可以使用 TRANSLATE 函数。

SQL> ed
Wrote file afiedt.buf

  1  with x as (
  2    select '10' addr from dual union all
  3    select '100' from dual union all
  4    select '100A' from dual union all
  5    select '1050' from dual union all
  6    select '110' from dual union all
  7    select '200D' from dual union all
  8    select '20' from dual
  9  )
 10  select addr,
 11         to_number( translate( addr,
 12                               '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ',
 13                               '1234567890' ) ) addr_num,
 14         translate( addr,
 15                    'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',
 16                    'ABCDEFGHIJKLMNOPQRSTUVWXYZ') addr_str
 17    from x
 18   order by to_number( translate( addr,
 19                                  '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ',
 20                                  '1234567890' ) ),
 21            translate( addr,
 22                       'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',
 23*                      'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
SQL> /

ADDR   ADDR_NUM ADDR_STR
---- ---------- ----------------
10           10
20           20
100A        100 A
100         100
110         110
200D        200 D
1050       1050

7 rows selected.

The regular expression based solutions are more elegant. But assuming you want to first sort on the numeric component using a numeric sort and then sort on the character component using a character sort, you can also use the TRANSLATE function.

SQL> ed
Wrote file afiedt.buf

  1  with x as (
  2    select '10' addr from dual union all
  3    select '100' from dual union all
  4    select '100A' from dual union all
  5    select '1050' from dual union all
  6    select '110' from dual union all
  7    select '200D' from dual union all
  8    select '20' from dual
  9  )
 10  select addr,
 11         to_number( translate( addr,
 12                               '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ',
 13                               '1234567890' ) ) addr_num,
 14         translate( addr,
 15                    'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',
 16                    'ABCDEFGHIJKLMNOPQRSTUVWXYZ') addr_str
 17    from x
 18   order by to_number( translate( addr,
 19                                  '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ',
 20                                  '1234567890' ) ),
 21            translate( addr,
 22                       'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',
 23*                      'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
SQL> /

ADDR   ADDR_NUM ADDR_STR
---- ---------- ----------------
10           10
20           20
100A        100 A
100         100
110         110
200D        200 D
1050       1050

7 rows selected.
脱离于你 2024-12-05 15:53:56

实现此目的的唯一方法是将街道号码解析为数字和非数字部分,并将它们存储在单独的列中,以便您可以首先对数字部分(作为数字)进行索引(或排序),然后对非数字部分进行索引(或排序)。数字部分。也许可以编写一个表达式来执行此操作,但随后您会丢失任何索引(如果这很重要)。

在 Oracle 11 中,有一个 REGEXP_SUBSTR 函数可用于完成此操作,但我不确定 Oracle 10g 中是否存在该函数。

The only way to accomplish this is to parse the street numbers into numeric and non-numeric parts, and store them in separate columns so you can index (or order by) on the numeric part first (as a number) and then the non-numeric part. It may be possible to write an expression that does this, but then you lose any indexing (if that's important).

In Oracle 11 there's a REGEXP_SUBSTR function that could be used to accomplish this, but I'm not sure it exists in Oracle 10g.

失退 2024-12-05 15:53:56

据我所知,您有两个基本选择:
1. 强制字段采用一致的格式
2. 将数字部分和非数字部分分开

对于选项一,您可以将 '10' 更改为 '00010-''100A'< /code> 为 '00100A' 等。然后他们会自然地对自己进行排序。

对于选项二,您可以将 '10' 更改为 (10,''),将 '100A' 更改为 ( 100,'A')等。然后分别按两个部分(数字和非数字)进行排序。

无论哪种方式,您都需要了解(或告诉我们)更多有关数据的明确特征的信息。 (模式是否始终为 [数字][alpha],[alpha] 部分是否始终为 1 个字符长,您是否有建筑物名称等?)

You have two basic options that I am aware of:
1. Force the field to a consistent format
2. Separate the numeric and non-numeric portions out

For option one, you would change '10' to be '00010-', and '100A' to be '00100A', etc. They will then naturally order themselves.

For option two, you would change '10' to be (10,''), and '100A' to be (100,'A'), etc. Then order by the two portions (numeric and non-numeric) separately.

Either way, you need to know (or tell us) a little bit more about the definitive characteristics of the data. (Is the pattern always [numeric][alpha], is the [alpha] portion always 1 character long, Do you ever have building Names, etc, etc?)

三生池水覆流年 2024-12-05 15:53:56

我没有使用 Oracle 的经验,但通常使用 SQL 类似的东西可能会有所帮助:

ORDER BY CAST(house AS integer), house

所以基本上你首先按字段的整数部分排序(CAST 应该忽略所有尾随非数字字符),如果它们相同,则应该将它们比较为字符串,因此 100A 将位于 100b 之前

显然这在 Oracle 中不起作用。查看评论

I have no experience with oracle but generally with SQL something like that could help:

ORDER BY CAST(house AS integer), house

So basically you order by integer part of the field first (CAST should disregard all trailing non numerical chars), and if they are the same it should compare them as a string so 100A would be before 100b

Apparently this will not work in Oracle. See comments

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文