2017-05-03 54 views
0

我有一个Case语句,它将给出范围值。我需要的结果范围明智的顺序进行相应的设置进行排序:Oracle SQL中的排序范围值

select distinct CASE 
    when Table__107.Column <= 30 then 
     '0-30' 
    when (Table__107.Column >= 31 and 
      Table__107.Column <= 60) then 
     '31-60' 
    when (Table__107.Column >= 61 and 
      Table__107.Column <= 90) then 
     '61-90' 
    when (Table__107.Column >= 91 and 
      Table__107.Column <= 120) then 
     '91-120' 
    when (Table__107.Column >= 121 and 
      Table__107.Column <= 180) then 
     '121-180' 
    when (Table__107.Column >= 181 and 
      Table__107.Column <= 365) then 
     '181-365' 
    when Table__107.Column > 365 then 
     '365+' 
    end as Column 

我所需的输出是

0-30 
31-60 
61-90 
..... 
..... 
365+ 

标志

我尝试使用Order by 1Order By ASC,但它是基于第一划分字符字母数字不是数字值的范围。

+0

我试过使用Order BY 1和Order By ASC,但它是基于第一个字符进行排序的。 – user1838000

回答

1

首先,您可以将您的范围重新命名为可排序。 。 。 000-030,'031-060'等等。然后您可以直接按价值订购。

接下来,您可以简化逻辑,因为您知道案例条件按顺序进行评估。

最后,你可以得到你想要的东西使用group by,然后订购后记中,每个组中的最小值说:

select (case when Table__107.Column <= 30 then '0-30' 
      when Table__107.Column <= 60 then '31-60' 
      when Table__107.Column <= 90 then '61-90' 
      when Table__107.Column <= 120 then '91-120' 
      when Table__107.Column <= 180 then '121-180' 
      when Table__107.Column <= 365 then '181-365' 
      when Table__107.Column > 365 then '365+' 
     end) as Column, 
     count(*) -- this is a guess 
from t 
group by (case when Table__107.Column <= 30 then '0-30' 
       when Table__107.Column <= 60 then '31-60' 
       when Table__107.Column <= 90 then '61-90' 
       when Table__107.Column <= 120 then '91-120' 
       when Table__107.Column <= 180 then '121-180' 
       when Table__107.Column <= 365 then '181-365' 
       when Table__107.Column > 365 then '365+' 
      end) 
order by min(Table__107.Column); 
2

只需创建第二列 - 用同样的标准(件),但订单号,然后按该列排序。

事情是这样的:

select distinct CASE 
    when Table__107.Col <= 30 then 
     '0-30' 
    when (Table__107.Col >= 31 and 
      Table__107.Col <= 60) then 
     '31-60' 
    when (Table__107.Col >= 61 and 
      Table__107.Col <= 90) then 
     '61-90' 
    when (Table__107.Col >= 91 and 
      Table__107.Col <= 120) then 
     '91-120' 
    when (Table__107.Col >= 121 and 
      Table__107.Col <= 180) then 
     '121-180' 
    when (Table__107.Col >= 181 and 
      Table__107.Col <= 365) then 
     '181-365' 
    when Table__107.Col > 365 then 
     '365+' 
    end as Col, CASE 
    when Table__107.Col <= 30 then 
     1 
    when (Table__107.Col >= 31 and 
      Table__107.Col <= 60) then 
     2 
    when (Table__107.Col >= 61 and 
      Table__107.Col <= 90) then 
     3 
    when (Table__107.Col >= 91 and 
      Table__107.Col <= 120) then 
     4 
    when (Table__107.Col >= 121 and 
      Table__107.Col <= 180) then 
     5 
    when (Table__107.Col >= 181 and 
      Table__107.Col <= 365) then 
     6 
    when Table__107.Col > 365 then 
     7 end as ord 
    from Table__107 
    order by ord 
+0

重复的逻辑很丑陋 – APC

2

时间桶看起来像数据的排序,值得参考的数据表。但即使你不想为他们创建一张桌子,也可以将它们视为一体。

在此解决方案中,我使用WITH子句为每个时间段创建一个具有排序号,下限和上限以及标签的子查询。这样做可以更容易地(说)计数Table__107.col1秋季如何实例中每个桶:

with buckets as ( 
    select 1 as bno, 0 as lb, 30 as ub, '0-30' as label from dual union all 
    select 2 as bno, 31 as lb, 60 as ub, '31-60' as label from dual union all 
    select 3 as bno, 61 as lb, 90 as ub, '61-90' as label from dual union all 
    select 4 as bno, 91 as lb, 120 as ub, '91-120' as label from dual union all 
    select 5 as bno, 121 as lb, 180 as ub, '121-180' as label from dual union all 
    select 6 as bno, 181 as lb, 365 as ub, '181-365' as label from dual union all 
    select 7 as bno, 365 as lb, 10000 as ub, '365+' as label from dual) 
select count(t107.col1) as col1_cnt 
     , b.label as bucket 
from buckets b 
     left outer join Table__107 t107 
      on t107.col1 between b.lb and b.ub 
group by b.bno, b.label 
order by b.bno  ; 

没有必要重复的逻辑。另一个好处是我们可以使用外连接,并捕获空桶。