有没有办法检查结果是否为小数?

时间:2016-01-26 20:49:03

标签: oracle decimal

我正在尝试从Oracle DB创建报告。

有没有办法检查结果是否为十进制,如果是,请将其放在另一列?

我要做的是:

  1. 如果订购商品的数量在一个范围内,请将其放在指定的列中。
  2. 如果该项目数为小数,请输入小数列。
  3. 到目前为止我的代码:

    SELECT 
      D5.ORD_NUM
    
    , CASE
          WHEN (D5.ORD_QTY/M.ITEM_BKD_QTY) < 6
          THEN (D5.ORD_QTY/M.ITEM_BKD_QTY)
          ELSE 0
      END NO_DISCOUNT
    , CASE
          WHEN ((D5.ORD_QTY/M.ITEM_BKD_QTY) >= 6 AND
                (D5.ORD_QTY/M.ITEM_BKD_QTY) <= 10)
          THEN (D5.ORD_QTY/M.ITEM_BKD_QTY)
          ELSE 0
      END DISCOUNT_1
    , CASE
          WHEN ((D5.ORD_QTY/M.ITEM_BKD_QTY) >= 11 AND
                (D5.ORD_QTY/M.ITEM_BKD_QTY) <= 20)
          THEN (D5.ORD_QTY/M.ITEM_BKD_QTY)
          ELSE 0
      END DISCOUNT_2
    , CASE
          WHEN ((D5.ORD_QTY/M.ITEM_BKD_QTY) >= 21 AND
                (D5.ORD_QTY/M.ITEM_BKD_QTY) <= 30)
              THEN (D5.ORD_QTY/M.ITEM_BKD_QTY)
          ELSE 0
          END DISCOUNT_3
    
    , D5.ITEM_NO
    , D5.ORD_LINE_CONF_DATE 
    
    FROM
         ORD D5, ITEM M
    
    WHERE D5.ITEM_NO = M.ITEM_CODE
      AND M.ITEM_BKD_LEV = 2
      AND D5.COMP_CODE = M.COMP_CODE
      AND D5.COMP_CODE = 'W1'
      AND D5.CUST_CODE = 'CUST1'
      AND D5.ITEM_STAT NOT LIKE 'dummy'
      AND D5.ORD_CONF_DATE IS NOT NULL
    

    我需要什么: 我需要添加一个条件,如果结果是小数,那么它应该完全进入另一个文件夹。 所以要添加到现有的CASE语句中,我需要这样的东西..

    , CASE
          WHEN (D5.ORD_QTY/M.ITEM_BKD_QTY) < 6
               AND
               (D5.ORD_QTY/M.ITEM_BKD_QTY) IS NOT DECIMAL 
          THEN (D5.ORD_QTY/M.ITEM_BKD_QTY)
          ELSE 0
      END NO_DISCOUNT
    

    最后,我需要添加一个CASE语句,将十进制值放在该列中。

    , CASE
      WHEN (D5.ORD_QTY/M.ITEM_BKD_QTY) IS DECIMAL
      THEN (D5.ORD_QTY/M.ITEM_BKD_QTY)
      ELSE 0
      END EXTRA_CHARGE
    

    所以,我理想的结果应该是这样的:

    +--------+------------+-----------+-----------+-----------+------------+
    |ORD_NUM |NO_DISCOUNT |DISCOUNT_1 |DISCOUNT_2 |DISCOUNT_3 |EXTRA_CHARGE|
    +--------+------------+-----------+-----------+-----------+------------+
    |ORDER 1 |           5|          0|          0|          0|           0|
    +--------+------------+-----------+-----------+-----------+------------+
    |ORDER 2 |           0|          8|          0|          0|           0|
    +--------+------------+-----------+-----------+-----------+------------+
    |ORDER 3 |           0|          0|         13|          0|           0|
    +--------+------------+-----------+-----------+-----------+------------+
    |ORDER 4 |           0|          0|          0|         25|           0|
    +--------+------------+-----------+-----------+-----------+------------+
    |ORDER 5 |           0|          0|          0|          0|         7.6|
    +--------+------------+-----------+-----------+-----------+------------+
    

3 个答案:

答案 0 :(得分:2)

您没有发布表结构,所以我创建了一个简化版本来显示生成它的逻辑:

SQL> with t (ord_qty, item_bkd_qty) as (
  2  select 10, 2 from dual union all
  3  select 10, 3 from dual union all
  4  select 13, 13 from dual union all
  5  select 33, 11 from dual
  6  )
  7  SELECT ord_qty,
  8         item_bkd_qty,
  9         (ord_qty / item_bkd_qty) do_math,
 10         CASE WHEN trunc(ord_qty / item_bkd_qty) = (ord_qty / item_bkd_qty)
 11              THEN          'IS NOT DECIMAL' END case_test
 12    FROM t;
   ORD_QTY ITEM_BKD_QTY    DO_MATH CASE_TEST
---------- ------------ ---------- --------------
        10            2          5 IS NOT DECIMAL
        10            3 3,33333333 
        13           13          1 IS NOT DECIMAL
        33           11          3 IS NOT DECIMAL

离开这里应该不错。如果您需要进一步的帮助,请提供一些创建表和插入语句,我们将能够更接近您的样本结果。

答案 1 :(得分:1)

有几种方法可以做到这一点:

(D5.ORD_QTY/M.ITEM_BKD_QTY) IS NOT DECIMAL

第一:

MOD(D5.ORD_QTY/M.ITEM_BKD_QTY,1) != 0

第二

TRUNC((D5.ORD_QTY/M.ITEM_BKD_QTY)) != (D5.ORD_QTY/M.ITEM_BKD_QTY)

答案 2 :(得分:1)

Mathew提供的MOD没有用,不知道为什么。

这对我有用:

CASE WHEN trunc(ord_qty / item_bkd_qty) = (ord_qty / item_bkd_qty)
     THEN IS NOT DECIMAL
END DISCOUNT
.
.
.
.
CASE WHEN trunc(ord_qty / item_bkd_qty) != (ord_qty / item_bkd_qty)
     THEN DECIMAL
END EXTRA_CHARGE