如果客户在接下来的 3 个月内购买了商品,请输入 1 sql 助理

put a 1 if the customer has bought in 3 following months sql assistant

我是 teradata SQL 助手的初学者,我不知道它是否可以满足我的需要。

我有一个包含变量 ID、月份(或期间)和当月收入的基数。我需要做的是,如果客户在接下来的 3 个月内购买,则为 1,如果没有,则为 0,并对所有 ID 执行此操作。例如,如果我在第 1 个月并且在接下来的 3 个月内有一次购买,那么在该行中为该客户输入 1。末期因为没有3个月,出现NA。

示例数据的代码如下:

IF OBJECT_ID('tempdb..#StackTest') IS NOT NULL
    DROP TABLE #StackTest;

CREATE TABLE #StackTest
(Id int
,Month int
,Income int
);

INSERT INTO #StackTest
(Id
,Month
,Income
)
VALUES
(1, 1, 5000),
(1, 2, 0),
(1, 3, 0),
(1',4, 0),
(1,5, 0),
(1,6, 0),
(1, 7, 400),
(1, 8, 0),
(1, 9, 0),
(1, 10, 0),
(1, 11, 0),
(1, 12, 0),
(1, 13, 400),
(2, 1, 5000),
(2, 2, 0),
(2, 3, 100),
(2,4, 0),
(2,5, 0),
(2,6, 0),
(2, 7, 0),
(2, 8, 1500),
(2, 9, 0),
(2, 10, 0),
(2, 11, 0),
(2, 12, 100),
(2, 13, 750),
(3, 1, 0),
(3, 2, 0),
(3, 3, 0),
(3',4, 0),
(3,5, 700),
(3,6, 240),
(3, 7, 100),
(3, 8, 0),
(3, 9, 0),
(3, 10, 0),
(3, 11, 0),
(3, 12, 500),
(3, 13, 760);

 ID | Month | Incomes
  1 |    1   | 5000
  1 |    2   |    0
  1 |    3   |    0
  1 |    4   |    0
  1 |    5   |    0
  1 |    6   |    0
  1 |    7   |  400
  1 |    8   |  300
  1 |    9   |    0
  1 |   10   |    0
  1 |   11   |    0
  1 |   12   |    0
  1 |   13   |  400
  2 |    1   |    0
  2 |    2   |  100
  2 |    3   |    0
  2 |    4   |    0
  2 |    5   |    0
  2 |    6   |    0
  2 |    7   |    0
  2 |    8   | 1500
  2 |    9   |    0
  2 |   10   |    0
  2 |   11   |    0
  2 |   12   |  100
  2 |   13   |  750
  3 |    1   |    0
  3 |    2   |    0
  3 |    3   |    0
  3 |    4   |    0
  3 |    5   |  700
  3 |    6   |  240
  3 |    7   |  100
  3 |    8   |    0
  3 |    9   |    0
  3 |   10   |    0
  3 |   11   |    0
  3 |   12   |  500
  3 |   13   |  760

我不得不用 R 来做, 他们可以帮助我,但现在我必须用 teradata sql 助手来做。

这就是我想要的:

 ID | Month | Incomes | Quarterly
  1 |    1   | 5000     |    0
  1 |    2   |    0     |    0
  1 |    3   |    0     |    0
  1 |    4   |    0     |    1
  1 |    5   |    0     |    1
  1 |    6   |    0     |    1
  1 |    7   |  400     |    1
  1 |    8   |  300     |    0
  1 |    9   |    0     |    0
  1 |   10   |    0     |    0
  1 |   11   |    0     |   NA
  1 |   12   |    0     |   NA
  1 |   13   |  400     |   NA
  2 |    1   |    0     |    1
  2 |    2   |  100     |    0
  2 |    3   |    0     |    0
  2 |    4   |    0     |    0
  2 |    5   |    0     |    1
  2 |    6   |    0     |    1
  2 |    7   |    0     |    1
  2 |    8   | 1500     |    0
  2 |    9   |    0     |    1
  2 |   10   |    0     |    1
  2 |   11   |    0     |   NA
  2 |   12   |  100     |   NA
  2 |   13   |  750     |   NA
  3 |    1   |    0     |    0
  3 |    2   |    0     |    1
  3 |    3   |    0     |    1
  3 |    4   |    0     |    1
  3 |    5   |  700     |    1
  3 |    6   |  240     |    1
  3 |    7   |  100     |    0
  3 |    8   |    0     |    0
  3 |    9   |    0     |    1
  3 |   10   |    0     |    1
  3 |   11   |    0     |   NA
  3 |   12   |  500     |   NA
  3 |   13   |  760     |   NA

这是我的尝试,但显然失败了,没有达到我的预期。

select Id,Month,Incomes, SUM(Incomes) OVER (PARTITION BY Id ORDER BY Month ROWS 3 PRECEDING) AS Quarterly from rentability  order by Id, Month

*可出租性是 table 创建的。

有人知道如何用 1 或那个时期的最大值来标记吗?谢谢!

考虑:

select 
    t.*,
    case when sum(income) over(
        partition by id 
        order by month 
        range between 1 following and 3 following
    ) > 0 
        then 1 
        else 0
    end quaterly
from StackTest t 

这通过在接下来的 3 个月中执行 window 求和来实现(我们使用范围定义而不是行定义,因此即使您缺少记录也应该可以实现)。