Postgresql:LAG直到找到具有特定值的行,并返回该值

时间:2018-02-27 09:55:16

标签: sql postgresql

我有一个包含来自客户的电子邮件的数据集。

其中一列是Type。在Type中存在一个值'Duplicate Case',它标记客户刚刚向我们发送了关于同一主题的大量电子邮件。我们只回复原文,并将所有其他案例作为重复内容关闭。但我想做的是获取原始电子邮件的类型。

我希望能够创建原始类型列:

enter image description here

每个客户的案例一般不超过5个。我想添加仅返回结果的逻辑,其中重复案例比原始案例早24小时

我有这段可怕的代码:

CASE
 WHEN type = 'Duplicate Case'
 AND LAG(type,4) OVER (PARTITION BY c.client_code ORDER BY case_number ASC) = 'Duplicate Case'
  THEN LAG(type,5) OVER (PARTITION BY c.client_code ORDER BY case_number ASC)

 WHEN type = 'Duplicate Case'
 AND LAG(type,3) OVER (PARTITION BY c.client_code ORDER BY case_number ASC) = 'Duplicate Case'
  THEN LAG(type,4) OVER (PARTITION BY c.client_code ORDER BY case_number ASC)

 WHEN type = 'Duplicate Case'
 AND LAG(type,2) OVER (PARTITION BY c.client_code ORDER BY case_number ASC) = 'Duplicate Case'
  THEN LAG(type,3) OVER (PARTITION BY c.client_code ORDER BY case_number ASC)

WHEN LAG(type) OVER (PARTITION BY c.client_code ORDER BY case_number ASC) = 'Duplicate Case'
AND type = 'Duplicate Case'
  THEN LAG(type,2) OVER (PARTITION BY c.client_code ORDER BY case_number ASC)

WHEN type = 'Duplicate Case'
  THEN LAG(type) OVER (PARTITION BY c.client_code ORDER BY case_number ASC)
    END AS original_type

这给了我一些我想要的东西:

enter image description here

但我怎样才能添加时间逻辑?如果在原始类型之前不到24小时创建此副本,我想仅从第一个非重复中输入Type?

1 个答案:

答案 0 :(得分:2)

所以你需要做的是创建一个带有空白字段的临时表,然后稍后用一些标准更新这个空白字段:

/* Main table */

DROP TABLE IF EXISTS cases;
CREATE TEMPORARY TABLE cases AS
SELECT
created_date,
type,
CASE WHEN type = 'Duplicate Case'
  THEN CAST('Unknown' AS VARCHAR(40))
  ELSE type END AS original_type,
 CAST('' AS VARCHAR(40)) AS original_case_number,
client_code,
case_number
FROM case
 ORDER BY c.client_code DESC, case_number DESC;

/* Append previous case data */

UPDATE cases
SET original_case_number = prev_case_number
FROM
(
    SELECT
      a.case_number,
      MAX(b.case_number) AS prev_case_number
      FROM cases a
     LEFT JOIN cases b ON a.client_code = b.client_code
     AND b.created_date BETWEEN a.created_date - INTERVAL '48 hour' AND 
     a.created_date + INTERVAL '1 second'
     AND b.type <> 'Duplicate Case'
     AND a.type = 'Duplicate Case'
     GROUP BY 1
 ) prev
  WHERE cases.case_number = prev.case_number;

UPDATE cases
SET original_type = b.type
FROM (SELECT *
      FROM cases) b
  WHERE cases.original_case_number = b.case_number;

SELECT * FROM个案