如何忽略字符串中第四个用 “|” 分隔的词后面的所有词?

huangapple go评论90阅读模式
英文:

How do I ignore any words after the 4th word in string which is seperated by '|'?

问题

我会感激任何人的帮助。

这是我之前问题的后续: https://stackoverflow.com/questions/76681531/how-can-i-have-multiple-if-statements-with-multiple-result-value-in-bq/76681963#76681963

这个方法只适用于我的category_tags有4个单词的情况。

如何忽略剩下的值?第4个单词之后的所有内容如何处理?

类似这样的category_tags= animals|land-mammals|cats|domestic-cats|blackcat|cutecat

它应该忽略domestic-cats之后的所有内容

到目前为止,这是可以工作的代码:

SELECT SPLIT(category_tag,'|')[OFFSET(0)] category_name,
['', 'Parent', 'Child', 'Grandchild', 'Greatgrandchild'][OFFSET(ARRAY_LENGTH(SPLIT(category_tag, '|')))] AS category_type,
'/' || REGEXP_REPLACE(REPLACE(category_tag, '|', '/'), r'^([^/]+)(/)(.*)', r'\3\2\1') AS category
FROM your_table

英文:

I would appriciate it if anyone can help.

It is the follow up of my previous question in here: https://stackoverflow.com/questions/76681531/how-can-i-have-multiple-if-statements-with-multiple-result-value-in-bq/76681963#76681963

This is working okay but only if my category_tags has 4 words.

How can I ignore the rest values? any thing after 4th word?

something like category_tags= animals|land-mammals|cats|domestic-cats|blackcat|cutecat

It should ignore anything after domestic-cats

this is the qode which works so far:

SELECT SPLIT(category_tag,'|')[OFFSET(0)] category_name, 
  ['', 'Parent', 'Child', 'Grandchild', 'Greatgrandchild'][OFFSET(ARRAY_LENGTH(SPLIT(category_tag, '|')))] AS category_type,
  '/' || REGEXP_REPLACE(REPLACE(category_tag, '|', '/'), r'^([^/]+)(/)(.*)', r'') AS category 
FROM your_table

答案1

得分: 0

希望您可以将以下内容合并到您之前的问题的解决方案中

SELECT 
  (SELECT STRING_AGG(tag, '|' ORDER BY OFFSET) 
    FROM UNNEST(SPLIT(category_tag, '|')) tag WITH OFFSET
    WHERE OFFSET < 4
  ) AS category_tag
FROM your_table    

如果应用于示例数据

WITH your_table AS (
  SELECT 'archeology' category_tag UNION ALL
  SELECT 'geology|planet earth' UNION ALL
  SELECT 'the moon|space|astronomy' UNION ALL
  SELECT 'orcas|animals|marine mammals|dolphins' UNION ALL
  SELECT 'animals|land-mammals|cats|domestic-cats|blackcat|cutecat'
)

输出是

如何忽略字符串中第四个用 “|” 分隔的词后面的所有词?

英文:

Hope you can incorporate below into solution for your previous question

SELECT 
  (SELECT STRING_AGG(tag, &#39;|&#39; ORDER BY OFFSET) 
    FROM UNNEST(SPLIT(category_tag, &#39;|&#39;)) tag WITH OFFSET
    WHERE OFFSET &lt; 4
  ) AS category_tag
FROM your_table    

if applied to sample data

WITH your_table AS (
  SELECT &#39;archeology&#39; category_tag UNION ALL
  SELECT &#39;geology|planet earth&#39; UNION ALL
  SELECT &#39;the moon|space|astronomy&#39; UNION ALL
  SELECT &#39;orcas|animals|marine mammals|dolphins&#39; UNION ALL
  SELECT &#39;animals|land-mammals|cats|domestic-cats|blackcat|cutecat&#39;
)

output is

如何忽略字符串中第四个用 “|” 分隔的词后面的所有词?

huangapple
  • 本文由 发表于 2023年7月14日 04:45:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定