【问题标题】:Presto equivalent of CONCAT_WSPresto 相当于 CONCAT_WS
【发布时间】:2020-07-13 01:22:10
【问题描述】:

我正在 Presto 中寻找一个函数来连接两列和下划线之类的分隔符。

【问题讨论】:

    标签: sql presto string-concatenation


    【解决方案1】:

    您正在这里寻找array_join 函数,请参阅docs

    array_join(x, delimiter, null_replacement) → varchar

    使用 连接给定数组的元素 分隔符和替换空值的可选字符串。

    示例:
    列是 c1,c2 你当然可以添加更多:

    WITH  demo_table (c1,c2) AS 
        (SELECT * FROM  (VALUES  (1,2),(3,4),(5,null),(7,8) ))
    SELECT array_join(array[c1,c2], '_', 'NA')
    FROM demo_table
    

    结果将是:
    1_2
    3_4
    5_NA
    7_8

    【讨论】:

      【解决方案2】:

      处理:

      select concat_ws(',', col1, col2)
      

      你可以使用:

      select substr( concat(case when col1 is not null then ',' || col1 else '' end,
                            case when col2 is not null then ',' || col2 else '' end
      
                           ),
                      2
                   )
      

      这会将非 NULL 值连接成一个字符串。结果字符串将以逗号开头。 substr() 删除第一个字符。

      【讨论】:

      • presto 不支持字符串的逻辑运算符
      • @MTT 。 . .我修复了 concat 运算符 -- 而+ 不是 logical 运算符,它是一个 arithmetic 运算符。
      【解决方案3】:

      这已被添加到 PrestoSQL(现在的 Trino)几个版本:https://trino.io/docs/current/functions/string.html

      concat_ws(string0, string1, ..., stringN) → varchar#
      Returns the concatenation of string1, string2, ..., stringN using string0 as a separator. If string0 is null, then the return value is null. Any null values provided in the arguments after the separator are skipped.
      
      concat_ws(string0, array(varchar)) → varchar
      Returns the concatenation of elements in the array using string0 as a separator. If string0 is null, then the return value is null. Any null values in the array are skipped.
      

      【讨论】:

        【解决方案4】:
        select concat(col1, ',', col2) 
        

        【讨论】:

        • 。 .这个答案是不正确的。它不能正确处理 NULL 值。
        猜你喜欢
        • 2017-10-23
        • 2018-08-06
        • 2011-05-14
        • 2020-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多