impala sql,如果值的子集中存在字符串,则返回值

ckx4rj1h  于 2021-06-26  发布在  Impala
关注(0)|答案(1)|浏览(415)

我有一张table id 字段(不是主键)包含 1 或者 null . 在过去的几年中,任何给定的零件都可以使用一个或两个可能的选项多次输入。
我试图写一个语句,如果有一个 1 与select语句关联。有许多半重复的行,有些是 1 还有一些 null 但是如果有 1 ,我想回去 true ,如果只有 null 值,我要返回 false . 我不知道该怎么编码。
如果这是我的 SELECT part,id from table where part = "ABC1234" 陈述

part      id
ABC1234   1
ABC1234   null
ABC1234   null
ABC1234   null
ABC1234   1

我想写一个返回 true ,因为 1 至少存在于其中一行中。
我最接近这一点的方法是使用 CASE 声明,但我还不太清楚:

SELECT
a1.part part,  
CASE WHEN a2.id is not null             
  THEN
        'true'
  ELSE
        'false'
  END AS            id
from table.parts a1, table.ids a2 where a1.part = "ABC1234" and a1.key = a2.key;

我还审理了以下案件:

CASE WHEN exists
       (SELECT id from table.ids where id = 1)               
  THEN

但我错了 subqueries are not supported in the select list 对于上述 SELECT 语句,如何返回一行内容如下:

part      id
ABC1234   true
pgpifvop

pgpifvop1#

您可以使用条件聚合来检查零件是否至少有一个id为1的行。

SELECT part,'True' id
from parts
group by part
having count(case when id = 1 then 1 end) >= 1

若要在id均为空时返回false,请使用

select part, case when id_true>=1 then 'True'
                  when id_false>=1 and id_true=0 then 'False' end id
from (
SELECT part,
count(case when id = 1 then 1 end) id_true,
count(case when id is null then 1 end) id_false,
from parts
group by part) t

相关问题