我正在尝试将一个脚本组合在一起,以便根据每个访问ID的特定区域流更新ActionTypeID列。
有三个区域,访问可以在任何区域之间移动,我需要根据ActionType表中的5个值更新ActionTypeID列。
更新的逻辑都是基于每个VisitID记录的Area列,在这里您可以看到VisitID 100从Area 1移动到2到3。而VisitID记录101从区域1移动到区域2,然后返回到区域1并向前移动到区域2,然后移动到区域3。请参阅“WhatActionTypeShouldBe”列。
样本数据
Create Table #SampleData
(
VisitID int,
StartDate datetime,
EndDate datetime,
Area tinyint,
ActionTypeID tinyint,
WhatActionTypeShouldBe tinyint
)
insert into #SampleData
(
VisitID,
StartDate,
EndDate,
Area,
ActionTypeID,
WhatActionTypeShouldBe
)
select
100,
'2020-01-26 00:16:09.800',
'2020-01-26 00:18:09.800',
1,
0,
1
union
select
100,
'2020-01-26 00:18:09.800',
'2020-01-26 00:21:09.800',
2,
0,
2
union
select
100,
'2020-01-26 00:21:09.800',
'2020-01-26 00:27:09.800',
3,
0,
3
union
select
101,
'2020-01-26 00:16:09.800',
'2020-01-26 00:18:09.800',
1,
0,
1
union
select
101,
'2020-01-26 00:18:09.800',
'2020-01-26 00:21:09.800',
2,
0,
2
union
select
101,
'2020-01-26 00:21:09.800',
'2020-01-26 00:24:09.800',
1,
0,
4
union
select
101,
'2020-01-26 00:24:09.800',
'2020-01-26 00:27:09.800',
2,
0,
2
union
select
101,
'2020-01-26 00:27:09.800',
'2020-01-26 00:30:09.800',
3,
0,
3
union
select
102,
'2020-01-26 00:24:09.800',
'2020-01-26 00:27:09.800',
2,
0,
2
union
select
102,
'2020-01-26 00:27:09.800',
'2020-01-26 00:30:09.800',
3,
0,
3
union
select
103,
'2020-01-26 00:24:09.800',
'2020-01-26 00:27:09.800',
1,
0,
1
union
select
103,
'2020-01-26 00:27:09.800',
'2020-01-26 00:30:09.800',
2,
0,
2
union
select
103,
'2020-01-26 00:30:09.800',
'2020-01-26 00:34:09.800',
3,
0,
3
union
select
103,
'2020-01-26 00:34:09.800',
'2020-01-26 00:36:09.800',
2,
0,
5
union
select
103,
'2020-01-26 00:36:09.800',
'2020-01-26 00:37:09.800',
3,
0,
3
Create Table #ActionType
(
ActionTypeID tinyint,
ActionTypeName varchar(50)
)
insert into #ActionType
(
ActionTypeID,
ActionTypeName
)
select
1,
'Visit Started'
union
select
2,
'Progress to Area 2'
union
select
3,
'Complete Visit in Area 3'
union
select
4,
'Return to Area 1'
union
select
5,
'Return to Area 2'
select * from #SampleData
select * from #ActionType
drop table #SampleData
drop table #ActionType