根据其他表中的匹配选择分区

时间:2016-09-19 15:39:42

标签: sql postgresql window-functions

拥有下表(conversations):

 id | record_id  |  is_response  |         text         |
 ---+------------+---------------+----------------------+
 1  |     1      |      false    | in text 1            |
 2  |     1      |      true     | response text 3      |
 3  |     1      |      false    | in text 2            |
 4  |     1      |      true     | response text 2      |
 5  |     1      |      true     | response text 3      |
 6  |     2      |      false    | in text 1            |
 7  |     2      |      true     | response text 1      |
 8  |     2      |      false    | in text 2            |
 9  |     2      |      true     | response text 3      |
 10 |     2      |      true     | response text 4      |

另一个帮助表(responses):

 id |         text         |
 ---+----------------------+
 1  | response text 1      |
 2  | response text 2      |
 3  | response text 4      |

我正在寻找输出以下内容的SQL查询:

  record_id |       context
  ----------+-----------------------+---------------------
       1    | in text 1 response text 3 in text 2 response text 2
  ----------+-----------------------+---------------------
       2    | in text 1 response text 1
  ----------+-----------------------+---------------------
       2    | in text 2 response text 3 response text 4

因此每次is_responsetruetext 响应表中,汇总对话上下文这一点,忽略了不以池中的响应结束的会话部分。

record_id 1中的生活响应文本3 上面的示例中。

我尝试了以下复杂的SQL,但有时会错误地汇总文本错误:

with context as(
    with answers as (

       SELECT record_id, is_response, id as ans_id
        , max(id)
          OVER (PARTITION BY record_id ORDER BY id
          ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS previous_ans_id
       FROM (select * from conversations where text in (select text from responses)) ans
       ),
     lines as (
      select answers.record_id, con.id, COALESCE(previous_ans_id || ',' || ans_id, '0') as block, con.text as text from answers, conversations con where con.engagement_id = answers.record_id and ((previous_ans_id is null and con.id <= ans_id) OR (con.id > previous_ans_id and con.id <= ans_id)) order by engagement_id, id asc
      )

      select record_id, block,replace(trim(both ' ' from string_agg(text, E' ')) ,'  ',' ') ctx from lines group by record_id, block order by record_id,block
      )

select * from context

我确信有更好的方法。

2 个答案:

答案 0 :(得分:1)

这是我的看法:

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);

这将扫描表conversations一次,但我不确定它是否会比您的解决方案表现更好。基本思想是使用窗口函数来计算同一记录中可能的前行如何,结束对话。然后,我们可以使用该号码和record_id进行分组,并放弃不完整的对话。

答案 1 :(得分:0)

有一个简单而快速的解决方案:

SELECT record_id, string_agg(text, ' ') As context
FROM  (
   SELECT c.*, count(r.text) OVER (PARTITION BY c.record_id ORDER BY c.id DESC) AS grp
   FROM   conversations  c
   LEFT   JOIN responses r ON r.text = c.text AND c.is_response
   ORDER  BY record_id, id
   ) sub
WHERE  grp > 0  -- ignore conversation part that does not end with a response
GROUP  BY record_id, grp
ORDER  BY record_id, grp;

count()仅计算非空值。如果r.textLEFT JOIN出现空白,则responses为空:

grp中的值(&#34; group&#34;的缩写)仅在触发新输出行时增加。属于同一输出行的所有行最终都具有相同的grp个数字。然后很容易在外部SELECT聚合。

特殊技巧是以相反的顺序计算会话结尾。最后结束后的所有内容(从最后开始时首先出现)获取grp = 0并在外部SELECT中删除。

类似案例有更多解释: