0

可能重复:
oracle PL/SQL:排序行

我运行这个查询:

 Select a.product, sum(
     case 
       when b.id=1 then round(c.sales,3)
       else 0 
     end) as Q1_2008,
 sum(
     case 
         when b.id=2 then round(c.sales,3) 
         else 0 
     end) as Q2_2008,
 sum(
     case
         when b.id=3 then round(c.sales,3) 
         else 0
     end) as Q3_2008
 from products a, quarters b, sales c
 where 
    a.id=c.PRODUCT_ID and 
    b.id=c.QUARTER_ID 
 group by a.product
 order by product

但我的产品栏不按顺序排列。

产品

PROD_1
PROD_10
PROD_2
PROD_3
PROD_4
PROD_5
PROD_6
PROD_7
PROD_8
PROD_9

PROD_10 应该在最后的第二行。我怎样才能解决这个问题?

4

5 回答 5

5

这是有序的。数字和字符串的排序方式不同。

11 在字符串中小于 2,因此将 product_10 放在您的 product_2 之前。如果您希望按字符串中的数字排序,则必须将该数字拉出并将其作为数字单独排序。

于 2012-05-29T13:07:08.207 回答
1

这是执行您想要的“order by”语句的示例:

order by cast(substr(product, 5, 100) as int)
于 2012-05-29T13:34:06.400 回答
1

使用 substr 来挑选字符串的数字部分。

使用 to_number 把它变成一个数字

按此排序。

于 2012-05-29T13:12:20.147 回答
0

What Limey said is correct. If you want to order the names correctly then either rename the strings so that they are padded by 0s...for example PROD_01, PROD_01, PROD_02, PROD_03, PROD_10. this way all will be in order.

else split, to_number and then Order the number part of the product.(this will only work in case you have common string as prefix as the product name)

于 2012-05-29T13:14:17.923 回答
0

如果您真的必须使用字符串(鉴于几分钟前已经提出了同样的问题),那么您可能必须走一些拜占庭拆分/转换路线。即取 _ 之前的子字符串,将其丢弃,将剩余部分转换为整数,按此排序。不过,我不想保证它是一个可持续的解决方案。

于 2012-05-29T13:11:14.137 回答