-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathCreateTag.sql
More file actions
59 lines (47 loc) · 1.58 KB
/
CreateTag.sql
File metadata and controls
59 lines (47 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
set ansi_nulls on
go
set quoted_identifier on
go
DROP Table IF EXISTS Tags
GO
DROP PROCEDURE IF EXISTS [CreateTag]
GO
/*
Description: This file creates the tag for the customer churn template.
*/
create procedure [CreateTag]
as
begin
-- Calculate total number of transactions by each customer
select UserId, count(TransactionId) as OverallProductsPurchased into #OverallActivities from Activities group by UserId
-- Calculate total number of transactions by each customer in the pre-churn period
select UserId, count(TransactionId) as PrechurnProductsPurchased
into Tags
from Activities
where TransactionTime<=dateAdd(day, -1*(select ChurnPeriod from ChurnVars), (select max(TransactionTime) from Activities))
group by UserId
-- Create tags: those with fewer transactions than a threshold in the churn period are tagged as churners. A binary version is also created.
alter table Tags add Tag varchar(10)
update Tags
set Tags.Tag = case
when ((OverallProductsPurchased-PrechurnProductsPurchased)<=(select ChurnThreshold from ChurnVars)) then 'Churner'
else 'Nonchurner'
end
from Tags
full join #OverallActivities
on Tags.UserId = #OverallActivities.UserId
alter table Tags add TagId varchar(10)
update Tags
set Tags.TagId = case
when ((OverallProductsPurchased-PrechurnProductsPurchased)<=(select ChurnThreshold from ChurnVars)) then '1'
else '0'
end
from Tags
full join #OverallActivities
on Tags.UserId = #OverallActivities.UserId
alter table Tags
drop column PrechurnProductsPurchased
end
go
execute CreateTag
go