【问题标题】:PostgreSQL Select rows based on combination of array valuesPostgreSQL 根据数组值的组合选择行
【发布时间】:2015-06-04 02:47:01
【问题描述】:

我想从我的数据库中选择所有行,其中一行包含一组单词/数组中的至少两个术语。

例如: 我有以下数组:

'{"test", "god", "safe", "name", "hello", "pray", "stay", "word", "peopl", "rain", "lord", "make", "life", "hope", "whatever", "makes", "strong", "stop", "give", "television"}'    

我得到了一个存储在数据库中的推文数据集。所以我想知道哪些推文(列名:tweet.content)至少包含 两个字词。

我当前的代码看起来像这样(当然它只选择一个词...):

CREATE OR REPLACE VIEW tweet_selection AS 
SELECT tweet.id, tweet.content, tweet.username, tweet.geometry,
FROM tweet
WHERE tweet.topic_indicator > 0.15::double precision
AND string_to_array(lower(tweet.content)) = ANY(SELECT '{"test", "god", "safe", "name", "hello", "pray", "stay", "word", "peopl", "rain", "lord", "make", "life", "hope", "whatever", "makes", "strong", "stop", "give", "television"}'::text[])

所以最后一行需要以某种方式进行调整,但我不知道如何 - 也许是内部连接?!

我将单词也以唯一的 id 存储在不同的表中。

我的一个朋友建议对每一行进行计数,但我没有在原始表中添加额外列的写入权限。

背景:

我将我的推文存储在 postgres 数据库中,并在数据集上应用了 LDA(潜在狄利克雷分配)。现在我得到了生成的主题和与每个主题相关的词(20 个主题和 25 个词)。

【问题讨论】:

  • @mu 太短 id = 整数
  • @mu 是数据库的标准公共架构太短... 推文结构 id = integer userid = bigint i> username = text tweetcontent_raw = text tweetcontent = text (stemmed tweets) tweetdate = _timestamp with time zone the_geom = geometry i> 存储单词的表(results_lda): oid topic_id = integer word = text topic_probability = double precision
  • sry 我的会话被中断了
  • 和 tweetcontent 的内容(这是我感兴趣的专栏)看起来是这样的:首先学习如何让自己快乐 ...仅作为示例...

标签: sql arrays postgresql select twitter


【解决方案1】:
select DISTINCT ON (tweet.id) tweet.id, tweet.content, tweet.username, tweet.geometry
from tweet
where
    tweet.topic_indicator > 0.15::double precision
    and (
        select count(distinct word)
        from
            unnest(
                array['test', 'god', 'safe', 'name', 'hello', 'pray', 'stay', 'word', 'peopl', 'rain', 'lord', 'make', 'life', 'hope', 'whatever', 'makes', 'strong', 'stop', 'give', 'television']::text[]
            ) s(word)
            inner join
            regexp_split_to_table(lower(tweet.content), ' ') v (word) using (word)
    ) >= 2

【讨论】:

  • 非常感谢!这解决了问题:) 我添加了SELECT DISTINCT ON (tweet.id) tweet.id, tweet.content .... 否则有重复的条目...非常感谢:)
猜你喜欢
  • 2016-02-21
  • 1970-01-01
  • 1970-01-01
  • 2019-09-07
  • 2018-07-17
  • 2022-01-23
  • 1970-01-01
  • 2018-09-07
  • 2019-09-18
相关资源
最近更新 更多