如何在Oracle SQL中对特定数字进行汇总?

时间:2015-09-30 13:54:46

标签: sql oracle

  1. 501 - > 588
  2. 588 - > 588
  3. 589 - > 688

4 个答案:

答案 0 :(得分:1)

可以使用负精度调用TRUNC和ROUND函数。所以我得到了百分之一,并且根据数字的其余部分是否大于88来增加88或188。

WITH data(val) AS (
    SELECT 88 FROM DUAL UNION ALL
    SELECT 89 FROM DUAL UNION ALL
    SELECT 501 FROM DUAL UNION ALL
    SELECT 588 FROM DUAL UNION ALL
    SELECT 1088 FROM DUAL UNION ALL
    SELECT 1000 FROM DUAL UNION ALL
    SELECT 1089 FROM DUAL UNION ALL
    SELECT 589 FROM DUAL
)
SELECT
    VAL,
    TRUNC(VAL, -2) + CASE WHEN MOD(val, 100) > 88 THEN 188 ELSE 88 END result 
FROM
    data

答案 1 :(得分:1)

我假设您希望新号码比下一个号码少12(所以:-12,88,188,288等)。

所以你先关闭,给你的数字加12,然后除以100.这将为你提供一个带小数的数字,你现在可以找到它的上限值。然后你需要将它乘以100并减去12.就像这样:

with sample_data as (select -12 num from dual union all
                     select -11 num from dual union all
                     select 88 num from dual union all
                     select 89 num from dual union all
                     select 173 num from dual union all
                     select 189 num from dual union all
                     select 501 num from dual union all
                     select 588 num from dual union all
                     select 589 num from dual)
select num,
       ceil((num + 12)/100)*100 -12 new_num
from   sample_data;


       NUM    NEW_NUM
---------- ----------
       -12        -12
       -11         88
        88         88
        89        188
       173        188
       189        288
       501        588
       588        588
       589        688

答案 2 :(得分:0)

不确定边界条件。此外,还不清楚价值范围。

似乎 CEIL 可能是一般解决方案。但是,根据问题中提供的当前值,您可以执行以下操作:

您可以使用 CASE 表达式。

例如,

SQL> WITH DATA(num) AS (
  2  SELECT 1 FROM DUAL UNION ALL
  3  SELECT 99 FROM DUAL UNION ALL
  4  SELECT 501 FROM DUAL UNION ALL
  5  SELECT 588 FROM DUAL UNION ALL
  6  SELECT 589 FROM DUAL UNION ALL
  7  SELECT 600 FROM DUAL UNION ALL
  8  SELECT 999 FROM DUAL
  9  )
 10  SELECT num,
 11    CASE
 12      WHEN num < 588
 13      THEN 588
 14      WHEN num > 588
 15      AND num  < 688
 16      THEN 688
 17      ELSE 999
 18    END new_num
 19  FROM DATA;

       NUM    NEW_NUM
---------- ----------
         1        588
        99        588
       501        588
       588        999
       589        688
       600        688
       999        999

7 rows selected.

SQL>

答案 3 :(得分:0)

另一个黑客,基于ROUND函数。

SQL Fiddle

<强>查询

with sample_data as (select -12 num from dual union all
                     select -11 num from dual union all
                     select 88 num from dual union all
                     select 89 num from dual union all
                     select 173 num from dual union all
                     select 189 num from dual union all
                     select 501 num from dual union all
                     select 588 num from dual union all
                     select 589 num from dual)
select num,
       /*-2 will round to nearest hundred.
       anything above 49 gets rounded up and we want to round up to 12 less than 100, 
       49 + 12 = 61, should be added to original number.
       */
       round(num + 61, -2) - 12 new_num
from   sample_data

<强> Results

| NUM |  NEW_NUM |
|-----|----------|
| -12 |      -12 |
| -11 |       88 |
|  88 |       88 |
|  89 |      188 |
| 173 |      188 |
| 189 |      288 |
| 501 |      588 |
| 588 |      588 |
| 589 |      688 |