Select partitions based on matches in other table

前端 未结 2 1818
灰色年华
灰色年华 2021-01-23 04:35

Having the following table (conversations):

 id | record_id  |  is_response  |         text         |
 ---+------------+---------------+------------         


        
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-23 04:37

    Here's my take:

    SELECT
        record_id,
        string_agg(text, ' ' ORDER BY id) AS context
    FROM (
        SELECT
            *,
            coalesce(sum(incl::integer) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING),0) AS grp
        FROM (
            SELECT *, is_response AND text IN (SELECT text FROM responses) as incl
            FROM conversations
             ) c
         ) c1
    GROUP BY record_id, grp
    HAVING bool_or(incl)
    ORDER BY max(id);
    

    This will scan the table conversations once, but I am not sure if it will perform better than your solution. The basic idea is to use a window function to count how maybe preceding rows within the same record, end the conversation. Then we can group by with that number and the record_id and discard incomplete conversations.

提交回复
热议问题