英文:
How do I ignore any words after the 4th word in string which is seperated by '|'?
问题
我会感激任何人的帮助。
这个方法只适用于我的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, '|' ORDER BY OFFSET)
FROM UNNEST(SPLIT(category_tag, '|')) tag WITH OFFSET
WHERE OFFSET < 4
) AS category_tag
FROM your_table
if applied to sample data
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'
)
output is
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论