Sql 不匹配时如何返回null(与nullif函数相反)

Sql 不匹配时如何返回null(与nullif函数相反),sql,oracle,Sql,Oracle,我想在两个exp不匹配时得到null,就像nullifexp1一样,exp2在相同时返回null,在不相同时返回第一个exp 我曾尝试过借助子查询或创建函数,但需要一种简单、简单、标准的解决方法 select * from table where service_type = 'X3' or (1 <> (select nvl(min(1),2) from table where service_type = 'X3') and service_type is null) 我想

我想在两个exp不匹配时得到null,就像nullifexp1一样,exp2在相同时返回null,在不相同时返回第一个exp

我曾尝试过借助子查询或创建函数,但需要一种简单、简单、标准的解决方法

select * from table
where service_type = 'X3' 
or (1 <> (select nvl(min(1),2) from table where service_type = 'X3') 
and service_type is null)
我想知道服务类型匹配X3的值是否返回X3,但如果不匹配X5,则返回null以匹配null记录

当服务类型='X3'

service_type
------------
X3
当服务类型='X5'或任何其他类似'X%'将返回null时

service_type
------------
null
什么时候开始呢

select case when service_type='x3' then service_type else null end
from table_name

我认为您需要一个子查询来实现实际要实现的逻辑:

select t.*
from t
where t.service_type = :value or
      (t.service_type is null and
       not exists (select 1 from t t2 where t2.service_type = :value)
      );
您还可以使用窗口功能:

select t.*
from (select t.*, count(*) over (partition by service_type) as cnt
      from t
      where service_type = :value or service_type is null
     ) t
where service_type = :value or
      (cnt = 0 and service_type is null);

然后,我必须从服务类型为'X3'的表中进行如下查询select*,或者在服务类型为'X3'时选择case,然后从表名称中选择服务类型else null end为null,服务类型为null,这也是管理的一个难题,我需要的不是nullifexp1,exp2在内联不匹配时返回nullquery@ShahbazAli请您分享有问题的样本数据和预期输出,或者我重新编辑的dbfiddle,或者您可以尝试:create table tblservice_type varchar220;插入tbl值'X3',插入tbl值'X4',插入tbl值在此处添加更多示例数据并指定预期的结果格式文本。您使用的是Oracle还是SQLite?Oracle数据库我已经完成了子查询,还有其他方法吗?
select t.*
from (select t.*, count(*) over (partition by service_type) as cnt
      from t
      where service_type = :value or service_type is null
     ) t
where service_type = :value or
      (cnt = 0 and service_type is null);