我希望看到两件事:
客户与我们之间的全面互动 直到那一点(来自账户的互动的总和是 数字大于一。)
基本上我正试图从附图中的顶层表到达底层表。
Customer month Accounts Interactions
12345 Jan-15 3 5
12345 Feb-15 3 1
12345 Mar-15 2 7
12345 Apr-15 1 3
12345 May-15 1 9
12345 Jun-15 1 2
12345 Jul-15 0 3
67890 Feb-15 1 4
67890 Mar-15 1 4
67890 Apr-15 1 9
67890 May-15 0 5
Customer Month close date Interactions
12345 Jul-15 30
67890 May-15 23
答案 0 :(得分:0)
当我第一次阅读这个问题时,听起来会有一个带窗口功能的简洁解决方案,但在重新阅读之后,我认为这是不必要的。假设关闭他的上一个帐户将是客户与您的最后一次交互,您只需要每个客户的最后交互日期,这意味着可以使用简单的聚合函数解决此问题:
SELECT customer, MAX(month), SUM(interactions)
FROM mytable
GROUP BY customer
答案 1 :(得分:0)
要获得最近三个月,您需要一个OLAP功能:
SELECT Customer, MAX(months), SUM(Interactions)
FROM
(
SELECT Customer, month, Interactions
FROM mytable
QUALIFY
-- only closed accounts
MIN(Accounts) OVER (PARTITION BY Customer) = 0
-- last three months
AND month >= oADD_MONTHS(MAX(month) OVER (PARTITION BY Customer), -3)
) AS dt
GROUP BY customer