Oracle View 中 Select 和除以零的问题

发布于 2024-08-15 02:36:36 字数 924 浏览 2 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

剑心龙吟 2024-08-22 02:36:36

或更短一点:

sw.INSTALLED_PORTS/NULLIF(sw.MAX_INSTALLABLE_PORTS,0)

问候,
抢。

or a little shorter:

sw.INSTALLED_PORTS/NULLIF(sw.MAX_INSTALLABLE_PORTS,0)

Regards,
Rob.

我为君王 2024-08-22 02:36:36

除以 NULL 与除以零不同(正如您在标题中引用的问题一样)。

从对偶中选择 1/null = null
select null/null from Dual = null

因此,您将通过(sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100自动获得您想要的内容。

我认为,问题出在 sw.MAX_INSTALLABLE_PORTS 为零时。在这种情况下,您可以使用以下内容:

case
  when sw.MAX_INSTALLABLE_PORTS = 0 then null
  else (sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100
end

Divizion by NULL is not the same as divizion by zero (as you reference the problem in the title).

select 1/null from dual = null
select null/null from dual = null

So 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:

case
  when sw.MAX_INSTALLABLE_PORTS = 0 then null
  else (sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100
end
等风也等你 2024-08-22 02:36:36
CASE
   WHEN sw.INSTALLED_PORTS IS NULL OR sw.MAX_INSTALLABLE_PORTS IS NULL THEN NULL
   ELSE (sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100
END UTIL_INFRASTRUCTURE_PORTS
CASE
   WHEN sw.INSTALLED_PORTS IS NULL OR sw.MAX_INSTALLABLE_PORTS IS NULL THEN NULL
   ELSE (sw.INSTALLED_PORTS/sw.MAX_INSTALLABLE_PORTS)*100
END UTIL_INFRASTRUCTURE_PORTS
鹿港巷口少年归 2024-08-22 02:36:36

您可以使用 COALESCE() ,它从其参数中返回第一个非空值,例如:

(COALESCE(sw.INSTALLED_PORTS, 0)/COALESCE(sw.MAX_INSTALLABLE_PORTS,1))*100  
 AS UTIL_DEVICE_PORTS

请注意,我使用 1 作为分母中的合并默认值,您不需要使用 DIVIDE/0 代替。

You could use COALESCE() which returns the first non-null from its arguments, like :

(COALESCE(sw.INSTALLED_PORTS, 0)/COALESCE(sw.MAX_INSTALLABLE_PORTS,1))*100  
 AS UTIL_DEVICE_PORTS

Note that I used 1 as the coalesce default in the denominator, you don't want a DIVIDE/0 instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文