首页 > 解决方案 > 寻找一些 SQL 帮助我有一个我想从中提取的列并且无法为其创建选择任何建议都会有所帮助

问题描述

有人可以帮我弄清楚如何编写一个选择语句来抓取像这样的列中@符号之后的单词任何帮助将不胜感激我在looker中使用sql runner @附加到我无法做到的单词上在帖子中

所以我永远不会知道需要拉多少个单词可能是 1 或 50,并且可能希望该列看起来像 revjahwar、nhl 等...我建立了这个联合但仍然可能有大量 @ 的 id 有拉出来,所以它不是太有效

@revjahwar #51&Done #21reasons #21dayswithPrime #IminmyPurpose #Purpose=Peace #iBelieve #Tiredofplayinggames #2019AintNobodyCare @ NFL

到目前为止,如果您关注评论,则有一种方法可以拉出迄今为止的第一个评论

标签: sqlsnowflake-cloud-data-platform

解决方案


Snowflake 的REGEXP_SUBSTR()函数“返回与字符串中的正则表达式匹配的子字符串”,这似乎是您在这里想要做的。这是一个例子。

with INSTAGRAM_POST_METRICS as (select $1 caption from values('@ revjahwar #51&Done #21reasons #21dayswithPrime #IminmyPurpose #Purpose=Peace #iBelieve #Tiredofplayinggames #2019AintNobodyCare @ NFL'))

select regexp_substr(
  caption,
  '@ ([^ ]+)',
  1,
  1,
  'e'
) word from INSTAGRAM_POST_METRICS;

word
revjahwar

这是获取所有@words的方法

with INSTAGRAM_POST_METRICS as (select $1 caption from values('@ revjahwar #51&Done #21reasons #21dayswithPrime #IminmyPurpose #Purpose=Peace #iBelieve #Tiredofplayinggames #2019AintNobodyCare @ NFL'))

SELECT   array_to_string(array_agg(word), ',') word_list
FROM     (
                SELECT caption,
                       split_part(t.value, ' ', 2) word
                FROM   instagram_post_metrics,
                       lateral flatten(split(caption, '@')) t
                WHERE  t.value != '')
GROUP BY caption;

推荐阅读