oracle:解码和子查询选择结果

发布于 2024-11-10 11:56:40 字数 531 浏览 3 评论 0原文

我有一个 Oracle 查询,其中一部分是使用 DECODE 计算一些值。例如:

SELECT ..., 
      (SELECT DECODE((SELECT 23 FROM DUAL), 
                      0, null,
                     (SELECT 23 FROM DUAL))  
         FROM DUAL) 
  FROM ...

这里的值“23”是在运行时计算的,并且它是相当复杂的连接 - 多个表,使用 PARTITION BY 等。因此,如果该值不是“,我想避免执行相同的子查询” 0”。有没有办法写这样的东西

SELECT ..., 
       (SELECT DECODE ((SELECT 23 FROM DUAL) as test, 
                        0, null,
                       test)  
         FROM DUAL) 
  FROM ...

I have a oracle query and part of it is calculating some value using DECODE. For example:

SELECT ..., 
      (SELECT DECODE((SELECT 23 FROM DUAL), 
                      0, null,
                     (SELECT 23 FROM DUAL))  
         FROM DUAL) 
  FROM ...

Here the value "23" gets calculated at runtime, and it's quite complicated joins - multiple tables, uses PARTITION BY etc. So I want to avoid executing the same subquery if the value is not "0". Is there any way to write something like this

SELECT ..., 
       (SELECT DECODE ((SELECT 23 FROM DUAL) as test, 
                        0, null,
                       test)  
         FROM DUAL) 
  FROM ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

青瓷清茶倾城歌 2024-11-17 11:56:40

这对你有用吗?
我刚刚将“23”移动到带有描述性别名的内联表中。

select ..., 
  (
   select 
     decode ( 
            computed_value.val, 
            0, null,
            computed_value.val
            )  
   from
     (select 23 as val from dual) computed_value
  )
from
  ...

CASE 语句也可能会增加清晰度,如下所示:

select
  ...
 ,case when computed_value.val = 0
       then null
       else computed_value.val
       end as my_field
from
  (select 23 as val from dual) computed_value
  ...

Will this work for you?
I've just moved the "23" to an inline table with a descriptive alias.

select ..., 
  (
   select 
     decode ( 
            computed_value.val, 
            0, null,
            computed_value.val
            )  
   from
     (select 23 as val from dual) computed_value
  )
from
  ...

A CASE statement might also add clarity, as in:

select
  ...
 ,case when computed_value.val = 0
       then null
       else computed_value.val
       end as my_field
from
  (select 23 as val from dual) computed_value
  ...
如梦亦如幻 2024-11-17 11:56:40

或者:

WITH q AS (
SELECT 23 test, 16 test2 FROM dual
)
SELECT ... 
     , DECODE(q.test, 0, NULL, q.test) value
     , CASE WHEN q.test2 = 0 THEN NULL 
            WHEN q.test2 = 16 THEN 1
            ELSE q.test2
       END another_value
  FROM q, ...

允许您在整个主选择中使用查询“q”(只要允许子查询)。称为WITH 子句、公共表表达式或子查询分解。请访问 Oracle-Base.com 了解更多相关信息。

Or:

WITH q AS (
SELECT 23 test, 16 test2 FROM dual
)
SELECT ... 
     , DECODE(q.test, 0, NULL, q.test) value
     , CASE WHEN q.test2 = 0 THEN NULL 
            WHEN q.test2 = 16 THEN 1
            ELSE q.test2
       END another_value
  FROM q, ...

Lets you use the query "q" throughout your main select, where ever a subquery is allowed. Called the WITH clause, or Common Table Expression, or Subquery Factoring. Read more about it at Oracle-Base.com.

赠佳期 2024-11-17 11:56:40

对于此特定场景,您可以使用 NULLIF 函数:

SELECT ..., 
      (SELECT NULLIF((SELECT 23 FROM DUAL), 0)  
         FROM DUAL) 
  FROM ...

如果两个参数相等,NULLIF 函数将返回 NULL,否则返回第一个参数。

For this particular scenario, you could use the NULLIF function:

SELECT ..., 
      (SELECT NULLIF((SELECT 23 FROM DUAL), 0)  
         FROM DUAL) 
  FROM ...

The NULLIF function returns NULL if the two arguments are equal, otherwise it returns the first argument.

我很坚强 2024-11-17 11:56:40

您可以在 from 子句中使用子查询并执行如下操作:

select conf_key, decode(test, 0, null, test) from (
select conf_key, (select conf_value from config_values where conf_key = 'DOMAINID') as TEST from config_values )

You can use the subquery in from clause and do something like below:

select conf_key, decode(test, 0, null, test) from (
select conf_key, (select conf_value from config_values where conf_key = 'DOMAINID') as TEST from config_values )
儭儭莪哋寶赑 2024-11-17 11:56:40

最好使用 CASE 语句。因为 CASE 语句就像一系列 IF 语句,仅使用关键字 WHEN。 CASE 语句是从上到下评估的。如果条件为真,则执行相应的 THEN 子句,并跳转到 END CASE(短路评估)子句。

Better you would have use CASE statement. since the CASE statement is like a series of IF statements, only using the key word WHEN. A CASE statement is evaluated from top to bottom. If a condition is true, then corresponding THEN clause is executed and execution jumps to the END CASE (short circuit evaluation) clause.

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