Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 购买客户的下一个日期_Sql_Sql Server - Fatal编程技术网

Sql 购买客户的下一个日期

Sql 购买客户的下一个日期,sql,sql-server,Sql,Sql Server,这里我需要客户下一个购买日期,请查看以下输出, 客户1已于2020-01-252020-01-262020-01-27购买(步骤=p) 我需要下一个购买日期,其中步骤=p,这意味着第二行的值为2020-01-26,第三行的值为2020-01-27,第六行的值应为空,因为2020-01-27日期后没有购买 输出 ID Date Zone Group destination start end sec step unit

这里我需要客户下一个购买日期,请查看以下输出, 客户1已于2020-01-252020-01-262020-01-27购买(步骤=p)
我需要下一个购买日期,其中步骤=p,这意味着第二行的值为2020-01-26,第三行的值为2020-01-27,第六行的值应为空,因为2020-01-27日期后没有购买

输出

ID   Date    Zone Group  destination  start        end                      sec    step   unit

1 2020-01-25 Zone1 GP1     D1         2020-01-25 08:18   2020-01-25 08:22   240      E     18

1 2020-01-25 Zone3 GP3     D1         2020-01-25 14:18   2020-01-25 14:22   198      P     50

1 2020-01-26 Zone3  GP3    D1         2020-01-26 14:18   2020-01-26 14:22   298      P     50

1 2020-01-27 Zone4 GP4     D1         2020-01-27 20:04   2020-01-27 20:09   283      D     55

1 2020-01-27 Zone3 GP3     D1         2020-01-27 08:18   2020-01-27 08:22   740      E     22

1 2020-01-27 Zone4 GP4     D1         2020-01-27 10:43   2020-01-27 10:57   853      p     289

2 2020-03-13 Zone1 GP1     D1         2020-03-13 08:08   2020-03-13 08:11   201      N     555 

2 2020-03-13 Zone2 Gp2     D1         2020-03-13 06:27   2020-03-13 06:40   767      E     789

2 2020-03-13 Zone3  GP3    D1         2020-03-13 21:02   2020-03-13 21:09   409      P     552

2 2020-03-15 Zone4 GP4     D1         2020-03-15 21:09   2020-03-15 21:10    78      P     253
您可以使用
lead()

ID Date       Zone  Group destination start             end    sec step unit NextPurchase_date  

1 2020-01-25  Zone1 GP1  D1   2020-01-25 08:18 2020-01-25 08:22  240  E 18  NULL

1 2020-01-25 Zone3 GP3   D1  2020-01-25 14:18 2020-01-25 14:22  98   P 50  2020-01-06  

1 2020-01-26 Zone3 GP3   D1  2020-01-26 14:18 2020-01-26 14:22  298  P 50  2020-01-27

1 2020-01-27 Zone4 GP4   D1 2020-01-27 20:04  2020-01-27 20:09  283  D 55  NULL

1 2020-01-27 Zone3 GP3   D1 2020-01-27 08:18  2020-01-27 08:22 740   E 22  NULL

1 2020-01-27 Zone4 GP4   D1 2020-01-27 10:43  2020-01-27 10:57 853   p 289  NULL

2 2020-03-13 Zone1 GP1   D1  2020-03-13 08:08 2020-03-13 08:11 201   N 555  NULL


2 2020-03-13 Zone2 Gp2   D1 2020-03-13 06:27 2020-03-13 06:40 767    E  789  NULL

2 2020-03-13 Zone3 GP3   D1 2020-03-13 21:02 2020-03-13 21:09 409    P  552  2020-03-15

2 2020-03-15 Zone4 GP4   D1 2020-03-15 21:09 2020-03-15 21:10 78     P 253 NULL
select t.*,
       (case when step = 'P'
             then convert(date, lead(start) over (partition by id, step order by start))
        end) as next_purchase_date
from t;