下面是我的数据
WH_QTY是可用的总数量,优先权是VIP,A,B,C。例如,应先将产品分配给VIP FIRST,然后再分配给A,B,C。
通过使用case语句,我已经能够获得VIP_Avail,A_Avail的正确值,但是转到B_Avail我收到以下错误。 “案例表达式只能嵌套到10级”
我要寻找的只是将VIP_Avail计算列用作我的A_Avail计算的基础,依此类推。但是,由于我要引用整个案例陈述,因此该陈述不成立。
以下是VIP_Avail的查询
IIF(ORDER_ITEMS.WH_OH =0,0,IIF(ORDER_ITEMS.WH_OH>=ORDER_AVAILABLE.VIP,ORDER_AVAILABLE.VIP,IIF(ORDER_ITEMS.WH_OH<ORDER_AVAILABLE.VIP,ORDER_ITEMS.WH_OH,0))) AS VIP_AVAIL
以下是对A_Avail的查询
CASE
WHEN
ORDER_ITEMS.WH_OH = 0 THEN 0
-- WHEN (WH_QTY-VIP)<A AND WH> 0 THEN (WH-VIP)
WHEN
((ORDER_ITEMS.WH_OH-IIF(ORDER_ITEMS.WH_OH =0,0,IIF(ORDER_ITEMS.WH_OH>=ORDER_AVAILABLE.VIP,ORDER_AVAILABLE.VIP,IIF(ORDER_ITEMS.WH_OH<ORDER_AVAILABLE.VIP,ORDER_ITEMS.WH_OH,0))))<A AND ORDER_ITEMS.WH_OH>0) THEN (ORDER_ITEMS.WH_OH-IIF(ORDER_ITEMS.WH_OH =0,0,IIF(ORDER_ITEMS.WH_OH>=ORDER_AVAILABLE.VIP,ORDER_AVAILABLE.VIP,IIF(ORDER_ITEMS.WH_OH<ORDER_AVAILABLE.VIP,ORDER_ITEMS.WH_OH,0))))
-- WHEN (WH_QTY - VIP) >= A THEN A
WHEN
(ORDER_ITEMS.WH_OH-IIF(ORDER_ITEMS.WH_OH =0,0,IIF(ORDER_ITEMS.WH_OH>=ORDER_AVAILABLE.VIP,ORDER_AVAILABLE.VIP,IIF(ORDER_ITEMS.WH_OH<ORDER_AVAILABLE.VIP,ORDER_ITEMS.WH_OH,0))))>=ORDER_AVAILABLE.A THEN ORDER_AVAILABLE.A
END AS A_AVAIL
谢谢, 陈
答案 0 :(得分:0)
假设您要更新表,我将执行以下操作:
UPDATE tickets_available
SET VIP_AVAIL = IIF(WH_OH > VIP, VIP, WH_OH),
A_AVAIL = IIF(WH_OH - VIP > A, A , if (WH_OH - VIP >= 0, WH_OH - VIP, 0)),
B_AVAIL = IIF(WH_OH - VIP - A > B, B, if (WH_OH - VIP - A >= 0, WH_OH - VIP - A, 0)),
C_AVAIL = IIF(WH_OH - VIP - A - B > C, C, if (WH_OH - VIP - A - B >= 0, WH_OH - VIP - A - B, 0))
UPDATE tickets_available
SET VIP_AVAIL = CASE WHEN WH_OH > VIP THEN VIP ELSE WH_OH END,
A_AVAIL = CASE WHEN WH_OH - VIP > A THEN A ELSE
CASE WHEN WH_OH - VIP >= 0 THEN WH_OH - VIP ELSE 0 END
END,
B_AVAIL = CASE WHEN WH_OH - VIP - A > B THEN B ELSE
CASE WHEN WH_OH - VIP - A >= 0 THEN WH_OH - VIP - A ELSE 0 END
END,
C_AVAIL = CASE WHEN WH_OH - VIP - A - B > C THEN C ELSE
CASE WHEN WH_OH - VIP - A - B >= 0 THEN WH_OH - VIP - A - B ELSE 0 END
END
我同时使用了IIF和CASE,因此您可以选择要使用的那个。最好您当然使用IIF,因为它更易读。
编辑:根据要求添加选择。
SELECT
Item,
WH_OH,
VIP,
IIF(WH_OH > VIP, VIP, WH_OH) AS VIP_AVAILABLE,
A,
IIF(WH_OH - VIP > A, A , if (WH_OH - VIP >= 0, WH_OH - VIP, 0)) AS A_AVAILABLE,
B,
IIF(WH_OH - VIP - A > B, B, if (WH_OH - VIP - A >= 0, WH_OH - VIP - A, 0)) AS B_AVAILABLE,
C,
IIF(WH_OH - VIP - A - B > C, C, if (WH_OH - VIP - A - B >= 0, WH_OH - VIP - A - B, 0)) AS C_AVAIL
FROM tickets_available