简洁的方式选择COST1与COST2不同的位置(请null = null)

发布于 2025-02-12 09:10:38 字数 1717 浏览 2 评论 0原文

我有两个列:COST1和COST2。

值可以:

  • 负数为零
  • null
with workorder
     (cost1,cost2) as (
select    1,    1 from dual union all
select -100, null from dual union all
select null,    0 from dual union all
select    0,  100 from dual union all
select null, null from dual
)

select
  *
from
  workorder
COST1COST2
11
-100NULL
NULL0 0
0100
NULLNULL

db<> fiddle


我想选择与cost1不同于cost2的行。

  • 我想对待无效,就像它们是平等的一样。
  • 但是我不想将无效视为零。

结果:

COST1COSS2
-100NULL
NULL0
0100

它等于不是 sqlite中的语法:

with workorder 
(cost1,cost2) as (
values
(   1,    1),
(-100, null),
(null,    0),
(   0,  100),
(null, null)
)

select
  *
from
  workorder
where
  cost1 is not cost2
COSS1 COSS1COSS2
-100NULL
NULL0
0100 100


norefloll (简洁的表现)

I have two number columns: COST1 and COST2.

Values can be:

  • Negative numbers
  • Zero
  • Positive numbers
  • Null
with workorder
     (cost1,cost2) as (
select    1,    1 from dual union all
select -100, null from dual union all
select null,    0 from dual union all
select    0,  100 from dual union all
select null, null from dual
)

select
  *
from
  workorder
COST1COST2
11
-100null
null0
0100
nullnull

db<>fiddle


I want to select rows where COST1 is different than COST2.

  • I want to treat nulls as if they are equal.
  • But I don't want to treat nulls as zeros.

Result:

COST1COST2
-100null
null0
0100

It would be equivalent to the is not syntax in SQLite:

with workorder 
(cost1,cost2) as (
values
(   1,    1),
(-100, null),
(null,    0),
(   0,  100),
(null, null)
)

select
  *
from
  workorder
where
  cost1 is not cost2
COST1COST2
-100null
null0
0100

db<>fiddle


How can I do that succinctly using Oracle SQL? (succinctness trumps performance)

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

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

发布评论

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

评论(3

兮子 2025-02-19 09:10:38

您可以使用 nvl 函数。因此检查不平等。

where NVL(CAST(cost1 AS VARCHAR2(10)), 'null') <> NVL(CAST(cost2 AS VARCHAR2(10)), 'null')

另一个选项是将检查零值的两个条件与单个检查结合在一起:两个值的串联应与一个或另一个成本值相对应。

where cost1 <> cost2
   OR CONCAT(cost1, cost2) IN (cost1, cost2)

检查demos

You can cast your values to strings, then transform your null values to "null" strings using the NVL function. Hence check the inequality.

where NVL(CAST(cost1 AS VARCHAR2(10)), 'null') <> NVL(CAST(cost2 AS VARCHAR2(10)), 'null')

Another option is combining your two conditions that check for null values with a single check: the concatenation of the two values should correspond to one or the other cost values.

where cost1 <> cost2
   OR CONCAT(cost1, cost2) IN (cost1, cost2)

Check the demos here.

烟花肆意 2025-02-19 09:10:38

如何使用binary_float_nan作为nvl的默认值?

with workorder (cost1,cost2) as (
    select    1,    1 from dual union all
    select -100, null from dual union all
    select null,    0 from dual union all
    select    0,  100 from dual union all
    select null, null from dual
)
select o.*
from   workorder o
where  nvl(cost1,binary_double_nan) <> nvl(cost2,binary_double_nan)

另外,如果为COPT定义了精度和比例,例如编号(10,2),则相当安全的默认值可能是0.0001 ,1/322/7

How about using binary_float_nan as the default for nvl?

with workorder (cost1,cost2) as (
    select    1,    1 from dual union all
    select -100, null from dual union all
    select null,    0 from dual union all
    select    0,  100 from dual union all
    select null, null from dual
)
select o.*
from   workorder o
where  nvl(cost1,binary_double_nan) <> nvl(cost2,binary_double_nan)

Alternatively, if precision and scale are defined for cost, for example number(10,2), then a reasonably safe default might be something like 0.0001, 1/3 or 22/7.

南街九尾狐 2025-02-19 09:10:38

这是一种方式。但这并不像我想要的那样简洁:

with workorder
     (cost1,cost2) as (
select    1,    1 from dual union all
select -100, null from dual union all
select null,    0 from dual union all
select    0,  100 from dual union all
select null, null from dual
)

select
  *
from
  workorder
where
  cost1 <> cost2
  or (cost1 is null and cost2 is not null)
  or (cost1 is not null and cost2 is null)
COST1  COST2
-----  -----
 -100   null
 null      0
    0    100

Here's one way. But it's not as succinct as I'd like:

with workorder
     (cost1,cost2) as (
select    1,    1 from dual union all
select -100, null from dual union all
select null,    0 from dual union all
select    0,  100 from dual union all
select null, null from dual
)

select
  *
from
  workorder
where
  cost1 <> cost2
  or (cost1 is null and cost2 is not null)
  or (cost1 is not null and cost2 is null)
COST1  COST2
-----  -----
 -100   null
 null      0
    0    100

db<>fiddle

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