2017-05-03 59 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  ; 

沒有必要重複的邏輯。另一個好處是我們可以使用外連接,並捕獲空桶。