SELECT
CASE
WHEN FIRST_NAME = 'alina' THEN (COMMISSION*RATE/TOTAL_DAYS)
ELSE REWARD
END AS reward
FROM testing;
我需要检查TOTAL_DAYS
是0
还是NULL
。我怎样才能做到这一点?对于NULL
的情况,我可以使用ISNULL
函数。
但是如何检查案例0
或NULL
?如果TOTAL_DAYS
ZERO
或NULL,
生成TOTAL_DAYS = 1
。
答案 0 :(得分:1)
select
CASE
WHEN FIRST_NAME = 'alina' THEN (COMMISSION * RATE/ISNULL(NULLIF(TOTAL_DAYS, 0), 1))
ELSE REWARD
end as reward
from testing;
答案 1 :(得分:1)
SELECT
CASE
WHEN FIRST_NAME = 'alina' THEN (COMMISSION * RATE/ ISNULL(NULLIF(TOTAL_DAYS, 0), 1))
ELSE REWARD
END ASreward
FROM testing;
如果NULLIF
为NULL
,TOTAL_DAYS
将返回0
。然后,如果ISNULL
返回1
,NULLIF
将返回NULL
。例如:
SELECT ISNULL(NULLIF(N, 0), 1) AS N
FROM(
VALUES (3), (NULL), (0)
)t(N)
返回:
N
-----------
3
1
1
答案 2 :(得分:0)
您可以使用coalesce
将null
翻译为1,将greatest
翻译为0:
SELECT CASE WHEN FIRST_NAME = 'alina'
THEN commission * rate / GREATEST(COALESCE(total_days, 1), 1)
ELSE reward END AS reward
FROM testing;
答案 3 :(得分:0)
如果有效,请检查一下。
select
CASE
WHEN FIRST_NAME = 'alina' THEN CASE WHEN COALESCE(TOTAL_DAYS,0) = 0 THEN 0 ELSE (COMMISSION*RATE/TOTAL_DAYS)
ELSE REWARD
end as reward
from testing;
答案 4 :(得分:0)
试试这个:
SELECT
CASE
WHEN FIRST_NAME = 'alina' THEN
commission * rate / COALESCE(NULLIF(total_days, 0), 1)
ELSE
reward
END AS reward
FROM
testing;