当我的查询在oracle上为空时,使用“case”获取字符串?

问题描述 投票:0回答:1

早上好,今天我有一个简单的问题: 我必须进行选择以获取是否有长度与数字不同的记录。我知道我的结果可能是一些记录或只是没有结果,因为将是空的,这是我的查询:

select CASE WHEN id IS null THEN 'no existen' ELSE 'existen' END AS existen
from dvd.cicle 
WHERE LENGTH(ID) <> (SELECT MAX(LENGTH(ID)) FROM dvd.cicle )

但我的结果看起来像这样:

enter image description here

我需要得到结果只是文本(如果存在或不存在)。有人可以帮助我吗?

问候

python sql database oracle pyspark
1个回答
0
投票

您提出的问题和您显示的查询不同。

你写

必须进行选择才能获取是否存在长度与数字不同的行

但是查询通常会检查是否存在具有不同 id 长度的行,而不是检查是否存在没有特定 id 长度的行。

那么你真正想要什么?

如果您通常想知道是否存在具有不同长度 id 的行,无论它们的长度是多少,您可以执行以下操作:

select 
  case when count(distinct length(id)) > 1 
  then 'exists' else 'not exists' end as existen
from cicle;

如果你想知道某个 id 的长度是否与数字不同,你可以这样做(这里,数字是 5):

select 
  case when count(case when length(id) <> 5 then 1 end) > 0 
  then 'exists' else 'not exists' end as existen
from cicle;

看到这个样本小提琴

© www.soinside.com 2019 - 2024. All rights reserved.