将@DavidAldridge 的建议放入代码中(并注意处理负值)可能如下所示:
WITH T AS
(
SELECT 2 AS X FROM DUAL
UNION ALL
SELECT -3 AS X FROM DUAL
UNION ALL
SELECT 4 AS X FROM DUAL
)
SELECT
CASE
-- If there are any zeroes, then the result is zero.
WHEN MAX(CASE WHEN X = 0 THEN 1 END) > 0 THEN 0
-- Otherwise, if there is an even (or zero) number of
-- negative numbers, the result is positive.
WHEN MOD(COUNT(CASE WHEN X < 0 THEN 1 END), 2) = 0 THEN 1
-- Otherwise, the result is negative.
ELSE -1
END
*
EXP
(
SUM
(
CASE
WHEN X = 0 THEN 0
ELSE LN(ABS(X))
END
)
)
FROM
T;
但是,需要注意的一件事是,与直接使用乘法的方法(如@NicholasKrasnov 建议的那些方法)相比,这种方法可能存在更大的舍入错误风险。