我正在为Oracle数据库编写SQL,其中包含用于计算的Case When语句。目前的方法如' 1'不会给我所有的整数,所以我正在寻找一种方法来说明NBR_UNITS / STD_PACK_QTY是否是一个整数然后'案例选择'如果它是小数点,则单位选择'。
下面是SQL现在的部分:
CASE
WHEN (NBR_UNITS/STD_PACK_QTY) like '1' THEN 'Case Pick'
WHEN (NBR_UNITS/STD_PACK_QTY) < '1' THEN 'Unit Pick'
ELSE 'Error'
END "Case Picks"
以下是数据示例。我试图让SQL输出&#39; Case Pick&#39;如果除法计算是任何整数,然后是“单位选择”。如果是小数点。
谢谢!
答案 0 :(得分:1)
检查任何数字表达式x
是否为整数的标准方法是
case when x = round(x) then ...
when x < 1 then...
else ... end as ....
(假设与1比较是正确的;不确定,我在原帖后面的评论中提出了澄清问题。)
答案 1 :(得分:1)
此特定示例的最简单解决方案是使用MOD
函数。
CASE
WHEN MOD(NBR_UNITS,STD_PACK_QTY) > 0 THEN 'Case Pick'
WHEN MOD(NBR_UNITS,STD_PACK_QTY) = 0 THEN 'Unit Pick'
END "Case Picks"
答案 2 :(得分:0)
将分区与分区的trunc / floor / round down进行比较。
WHEN (NBR_UNITS/STD_PACK_QTY) = TRUNC(NBR_UNITS/STD_PACK_QTY) THEN 'Case Pick'
不确定您的规则是否可以使用ELSE来捕获其他所有内容 - 例如,如果案例数量是24而他们给你36,那可以吗?如果是这样,那么ELSE基本上捕获了其他一切,对吧?如果这不合适,单位数量只适用于&lt; 24,然后ELSE (NBR_UNITS/STD_PACK_QTY) < 1
工作,ELSE可能是错误。
答案 3 :(得分:0)
由于整数超出9,因此您可以考虑使用TRUNC()函数。它删除了结果的小数部分。因此,当TRUNC(Result)等于Result时,Result为整数。
WITH
"Data" AS
(
SELECT 1 AS NBR_UNITS, 8 AS STD_PACK_QTY FROM dual
UNION
SELECT 2 AS NBR_UNITS, 8 AS STD_PACK_QTY FROM dual
UNION
SELECT 3 AS NBR_UNITS, 8 AS STD_PACK_QTY FROM dual
UNION
SELECT 4 AS NBR_UNITS, 8 AS STD_PACK_QTY FROM dual
UNION
SELECT 5 AS NBR_UNITS, 8 AS STD_PACK_QTY FROM dual
UNION
SELECT 6 AS NBR_UNITS, 8 AS STD_PACK_QTY FROM dual
UNION
SELECT 7 AS NBR_UNITS, 8 AS STD_PACK_QTY FROM dual
UNION
SELECT 8 AS NBR_UNITS, 8 AS STD_PACK_QTY FROM dual
UNION
SELECT 9 AS NBR_UNITS, 8 AS STD_PACK_QTY FROM dual
)
SELECT
NBR_UNITS
,STD_PACK_QTY
,NBR_UNITS/STD_PACK_QTY AS "Ratio"
,CASE
WHEN (TRUNC(NBR_UNITS/STD_PACK_QTY) = NBR_UNITS/STD_PACK_QTY) THEN 'Case Pick'
WHEN (TRUNC(NBR_UNITS/STD_PACK_QTY) < 1) THEN 'Unit Pick'
ELSE 'Error'
END AS "Case Picks"
FROM "Data"
;