如果金额> = $ 10M或< = $ - 10B,则将金额拆分为多行

时间:2015-07-02 21:48:07

标签: oracle

我在oracle数据库中有一个表,其中可能包含> = $ 10M或< = $ - 10B。

  1. 99999999.99块,还包括其余部分。
  2. 如果该值小于或等于$ -10B,我需要分成一个或多个999999999.99块并包含余数。

1 个答案:

答案 0 :(得分:0)

你的问题有些难以理解,但除非你没有提供示例,否则这可以帮助你或有类似问题的人。 假设您有这些数据,并且您希望将金额划分为不大于999的块:

id amount
-- ------
 1   1500
 2    800
 3   2500

此查询:

select id, amount, 
  case when level=floor(amount/999)+1 then mod(amount, 999) else 999 end chunk
  from data
  connect by level<=floor(amount/999)+1 
      and prior id = id and prior dbms_random.value is not null 

...除去金额,最后一行包含余数。输出是:

    ID     AMOUNT      CHUNK
------ ---------- ----------
     1       1500        999 
     1       1500        501 
     2        800        800 
     3       2500        999 
     3       2500        999 
     3       2500        502

SQLFiddle demo

编辑:完整查询根据其他说明:

select id, amount, 
    case 
      when amount>=0 and level=floor(amount/9999999.99)+1 then mod(amount, 9999999.99) 
      when amount>=0 then 9999999.99
      when level=floor(-amount/999999999.99)+1 then -mod(-amount, 999999999.99) 
      else -999999999.99 
    end chunk
  from data
  connect by ((amount>=0 and level<=floor(amount/9999999.99)+1) 
              or (amount<0 and level<=floor(-amount/999999999.99)+1))
         and prior id = id and prior dbms_random.value is not null

SQLFiddle

请根据需要调整正负边框(9999999.99和999999999.99)的数字。 有更多可能的解决方案(递归CTE查询,PLSQL过程,可能还有其他),这种分层查询就是其中之一。

相关问题