Oracle View 中 Select 和除以零的问题
我想在 Oracle 中创建一个视图来计算“利用率百分比”。
AS SELECT
sw.SWITCH_ID,
sw.ASSET_ID,
sw.SYSTEMNAME,
sw.MAX_INSTALLABLE_PORTS,
sw.INSTALLED_PORTS,
sw.USED_PORTS,
(sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100 AS UTIL_INSTALLED_PORTS,
sw.RES_INFRASTRUCTURE_PORTS,
sw.USED_INFRASTRUCTURE_PORTS,
sw.FREE_INFRASTRUCTURE_PORTS,
(sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100 AS UTIL_INFRASTRUCTURE_PORTS,
sw.RESERVED_DEVICE_PORTS,
sw.USED_DEVICE_PORTS,
sw.FREE_DEVICE_PORTS,
(sw.FREE_DEVICE_PORTS/sw.RESERVED_DEVICE_PORTS)*100 AS UTIL_DEVICE_PORTS,
sw.RUN_DATE
问题:有时 sw.INSTALLED_PORTS 或 sw.MAX_INSTALLABLE_PORTS 可以为 NULL(对于其他 UTIL 行也是如此)。
有没有什么好的方法可以做类似的事情:
if (sw.INSTALLED_PORTS or sw.MAX_INSTALLABLE_PORTS == null)
UTIL_INSTALLABLE_PORTS = null
else (sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100 AS UTIL_INSTALLABLE_PORTS,
I want to create an view in Oracle which calculates an "utilization rate in percent".
AS SELECT
sw.SWITCH_ID,
sw.ASSET_ID,
sw.SYSTEMNAME,
sw.MAX_INSTALLABLE_PORTS,
sw.INSTALLED_PORTS,
sw.USED_PORTS,
(sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100 AS UTIL_INSTALLED_PORTS,
sw.RES_INFRASTRUCTURE_PORTS,
sw.USED_INFRASTRUCTURE_PORTS,
sw.FREE_INFRASTRUCTURE_PORTS,
(sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100 AS UTIL_INFRASTRUCTURE_PORTS,
sw.RESERVED_DEVICE_PORTS,
sw.USED_DEVICE_PORTS,
sw.FREE_DEVICE_PORTS,
(sw.FREE_DEVICE_PORTS/sw.RESERVED_DEVICE_PORTS)*100 AS UTIL_DEVICE_PORTS,
sw.RUN_DATE
Problem: sometimes sw.INSTALLED_PORTS or sw.MAX_INSTALLABLE_PORTS can be NULL (same for other UTIL Rows).
Is there any nice way to do something like:
if (sw.INSTALLED_PORTS or sw.MAX_INSTALLABLE_PORTS == null)
UTIL_INSTALLABLE_PORTS = null
else (sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100 AS UTIL_INSTALLABLE_PORTS,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
或更短一点:
问候,
抢。
or a little shorter:
Regards,
Rob.
除以 NULL 与除以零不同(正如您在标题中引用的问题一样)。
从对偶中选择 1/null
= nullselect null/null from Dual
= null因此,您将通过
(sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100
自动获得您想要的内容。我认为,问题出在 sw.MAX_INSTALLABLE_PORTS 为零时。在这种情况下,您可以使用以下内容:
Divizion by NULL is not the same as divizion by zero (as you reference the problem in the title).
select 1/null from dual
= nullselect null/null from dual
= nullSo you'll automatically get what you want by
(sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100
.I think, the problem is when
sw.MAX_INSTALLABLE_PORTS
is zero. In this case you can use the following:您可以使用 COALESCE() ,它从其参数中返回第一个非空值,例如:
请注意,我使用 1 作为分母中的合并默认值,您不需要使用 DIVIDE/0 代替。
You could use COALESCE() which returns the first non-null from its arguments, like :
Note that I used 1 as the coalesce default in the denominator, you don't want a DIVIDE/0 instead.