Oracle排序数据包含文本&数

时间:2018-01-06 03:49:55

标签: sql oracle

我正在使用Oracle数据库,现在我想对包含文本和数字的数据进行排序。

以下数据的脚本:

SELECT * FROM WA_GA_TBL_LINES
  WHERE LINENAME LIKE 'LEGO%' AND
        SECTIONID_FK = 'SC0013' AND
        LINENAME != 'LEGO 16'
  ORDER BY LPAD(LINENAME,
           (SELECT MAX(LENGTH(LINENAME))
              FROM WA_GA_TBL_LINES)) ASC

数据

LEGO 1
LEGO 2
LEGO 3
LEGO 4
LEGO 5
LEGO 6
LEGO 7
LEGO 8
LEGO 9
LEGO 10
LEGO 11
LEGO 15
LEGO 12A
LEGO 12B
LEGO 13A
LEGO 13B
LEGO 14A
LEGO 14B

正如您所看到的,LEGO 15位置不正确。

我想要的是LEGO 15在最后一个位置(由于15是最高的数字)

LEGO 1
LEGO 2
LEGO 3
LEGO 4
LEGO 5
LEGO 6
LEGO 7
LEGO 8
LEGO 9
LEGO 10
LEGO 11
LEGO 12A
LEGO 12B
LEGO 13A
LEGO 13B
LEGO 14A
LEGO 14B
LEGO 15

4 个答案:

答案 0 :(得分:4)

使用REGEXP可以实现如下结果 -

LEGO 1
LEGO 2
LEGO 3
LEGO 4
LEGO 5
LEGO 6
LEGO 7
LEGO 8
LEGO 9
LEGO 10
LEGO 11
LEGO 12A
LEGO 12B
LEGO 13A
LEGO 13B
LEGO 14A
LEGO 14B
LEGO 15

<强>结果:

    import boto3
    import datetime
    client = boto3.client('ec2',region_name='us-west-1')
    snapshots = client.describe_snapshots(OwnerIds=['12345678'])
    for snapshot in snapshots['Snapshots']:
       a= snapshot['StartTime']
       b=a.date()
       c=datetime.datetime.now().date()
       d=c-b
       try:
        if d.days>10:
           id = snapshot['SnapshotId']
           client.delete_snapshot(SnapshotId=id)
       except Exception,e:
        if 'InvalidSnapshot.InUse' in e.message:
           print "skipping this snapshot"
           continue

答案 1 :(得分:3)

您需要对LINENAME中的数字和字母进行子字符串并进行排序

SELECT * FROM WA_GA_TBL_LINES 
WHERE LINENAME LIKE 'LEGO%' 
    AND SECTIONID_FK = 'SC0013' 
    AND LINENAME != 'LEGO 16' 
ORDER BY TO_NUMBER(SUBSTR(LINENAME,5, 3)),SUBSTR(LINENAME,8, 2)

您可以根据字母表来控制订单,当其为空且非空情况

ORDER BY TO_NUMBER(SUBSTR(LINENAME,5, 3)) ASC ,SUBSTR(LINENAME,8, 2) ASC NULLS FIRST

SQL小提琴

with legs(leg) as (
select 'LEGO 1' from dual union
select 'LEGO 2' from dual union
select 'LEGO 3' from dual union
select 'LEGO 4' from dual union
select 'LEGO 5' from dual union
select 'LEGO 6' from dual union
select 'LEGO 7' from dual union
select 'LEGO 8' from dual union
select 'LEGO 9' from dual union
select 'LEGO 10' from dual union
select 'LEGO 11' from dual union
select 'LEGO 15' from dual union
select 'LEGO 12A' from dual union
select 'LEGO 12B' from dual union
select 'LEGO 13A' from dual union
select 'LEGO 13B' from dual union
select 'LEGO 14A' from dual union
select 'LEGO 14B' from dual
) select leg from legs order by to_number(substr(leg,5, 3)),substr(leg,8, 2)

<强> Results

    |      LEG |
    |----------|
    |   LEGO 1 |
    |   LEGO 2 |
    |   LEGO 3 |
    |   LEGO 4 |
    |   LEGO 5 |
    |   LEGO 6 |
    |   LEGO 7 |
    |   LEGO 8 |
    |   LEGO 9 |
    |  LEGO 10 |
    |  LEGO 11 |
    | LEGO 12A |
    | LEGO 12B |
    | LEGO 13A |
    | LEGO 13B |
    | LEGO 14A |
    | LEGO 14B |
    |  LEGO 15 |

答案 2 :(得分:2)

首先,我不批评任何人;这只是另一种观点,从今天的第一杯咖啡看。我也希望这不是废话。

请注意,到目前为止发布的答案假设第一个单词始终是“LEGO”。那么,基于样本数据,它确实是。只有@OP知道真相,所以我也只是猜测,就像你一样。

但是,如果是这样,我怀疑数据模型可能没有正确规范化(即LEGO应该在自己的列中)。

如果“第一个单词”发生变化,请考虑在ORDER BY子句中稍作修改。例如(基于@ Saravana的代码;感谢您提供示例数据!):

SQL> with legs(leg) as (
  2  select 'LEGO 1' from dual union
  3  select 'LEGO 2' from dual union
  4  select 'LEGO 3' from dual union
  5  select 'LEGO 4' from dual union
  6  select 'LEGO 5' from dual union
  7  select 'LEGO 6' from dual union
  8  select 'LEGO 7' from dual union
  9  select 'LEGO 8' from dual union
 10  select 'LEGO 9' from dual union
 11  select 'TRIAGO 10' from dual union   --> here
 12  select 'LEGO 11' from dual union
 13  select 'LEGO 15' from dual union
 14  select 'LEGO 12A' from dual union
 15  select 'VIGO 12B' from dual union    --> here
 16  select 'LEGO 13A' from dual union
 17  select 'LEGO 13B' from dual union
 18  select 'LEGO 14A' from dual union
 19  select 'LEGO 14B' from dual
 20  )
 21  select leg from legs
 22  order by regexp_substr(leg, '^\w+'),
 23           to_number(regexp_substr(leg, '\d+')),
 24           regexp_substr(leg, '\w+$');

LEG
---------
LEGO 1
LEGO 2
LEGO 3
LEGO 4
LEGO 5
LEGO 6
LEGO 7
LEGO 8
LEGO 9
LEGO 11
LEGO 12A
LEGO 13A
LEGO 13B
LEGO 14A
LEGO 14B
LEGO 15
TRIAGO 10   --> here
VIGO 12B    --> here

18 rows selected.

SQL>

答案 3 :(得分:0)

我已使用这组数据运行查询,但它运行正常。

从A中选择*  按to_number排序(regexp_substr(regexp_substr(A,'[^] +',1,2),'[^ A-Z] +',1,1))  ASC;