简洁的方式选择COST1与COST2不同的位置(请null = null)
我有两个列: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
COST1 | COST2 |
---|---|
1 | 1 |
-100 | NULL |
NULL | 0 0 |
0 | 100 |
NULL | NULL |
我想选择与cost1不同于cost2的行。
- 我想对待无效,就像它们是平等的一样。
- 但是我不想将无效视为零。
结果:
COST1 | COSS2 |
---|---|
-100 | NULL |
NULL | 0 |
0 | 100 |
它等于不是
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 COSS1 | COSS2 |
---|---|
-100 | NULL |
NULL | 0 |
0 | 100 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
COST1 | COST2 |
---|---|
1 | 1 |
-100 | null |
null | 0 |
0 | 100 |
null | null |
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:
COST1 | COST2 |
---|---|
-100 | null |
null | 0 |
0 | 100 |
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
COST1 | COST2 |
---|---|
-100 | null |
null | 0 |
0 | 100 |
How can I do that succinctly using Oracle SQL? (succinctness trumps performance)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
nvl
函数。因此检查不平等。另一个选项是将检查零值的两个条件与单个检查结合在一起:两个值的串联应与一个或另一个成本值相对应。
检查demos
You can cast your values to strings, then transform your null values to "null" strings using the
NVL
function. Hence check the inequality.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.
Check the demos here.
如何使用
binary_float_nan
作为nvl
的默认值?另外,如果为
COPT
定义了精度和比例,例如编号(10,2)
,则相当安全的默认值可能是0.0001 ,
1/3
或22/7
。How about using
binary_float_nan
as the default fornvl
?Alternatively, if precision and scale are defined for
cost
, for examplenumber(10,2)
, then a reasonably safe default might be something like0.0001
,1/3
or22/7
.这是一种方式。但这并不像我想要的那样简洁:
Here's one way. But it's not as succinct as I'd like:
db<>fiddle