【问题标题】:使用 regexp_like 打印查询的结果
【发布时间】:2022-01-23 16:47:20
【问题描述】:

我有以单词point 开头的记录和不以单词point 开头的记录。

我试图计算每个有多少,我有这个查询:

select count(*) from tbl
where regexp_like(name, '.*point.*')

上面的查询正在运行。我想知道如何打印这样的结果:

names_with_point    names_without_point
  234                     120

【问题讨论】:

    标签: sql presto


    【解决方案1】:

    不确定 count_if 是一个 ANSI SQL 函数。

    select count(case when regexp_like(name, '.*point.*') THEN name end) as  names_with_point,
           count(case when not regexp_like(name, '.*point.*') THEN name end ) as names_without_point
              from dataset
    

    【讨论】:

    • OP 已根据文档使用支持 count_if 的 presto 标记了问题。
    • 是的,你是对的。
    【解决方案2】:

    你可以使用count_if:

    select count_if(regexp_like(name, '.*point.*')) as names_with_point
        , count_if(not regexp_like(name, '.*point.*')) as names_without_point
    from dataset
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多