-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFY18 Assignments Processing.sql
More file actions
334 lines (294 loc) · 15.7 KB
/
FY18 Assignments Processing.sql
File metadata and controls
334 lines (294 loc) · 15.7 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/****** Script for recording assignment changes (add/remove requests) ******/
use AMS_CSSO_BI
--delete from [AMS_CSSO_BI].[FY18].[tmpAssignmentSubmissions]
where empid is null
--Data quality check as employee id may be cut off
SELECT *
FROM [AMS_CSSO_BI].[FY18].[tmpAssignmentSubmissions]
where not LEN(isnull([EmpID],'bad')) = 8
--and not [Name Rep] is null
--update [FY18].[tmpAssignmentSubmissions] set [empid] = substring('000'+[EmpID],LEN([empid])-4,8 ) where LEN([EmpID])<8
--Data quality check for missing employees
SELECT distinct [EmpID],[Name Rep]
FROM [AMS_CSSO_BI].[FY18].[tmpAssignmentSubmissions]
where not isnull([EmpID],'!Data issue!') in
--(select [employee id] from [FY18].[SalesEmployeeMaster] )
(select [employee id] from [FY18].[SalesEmployeeMaster] where [Include]='True')
-- check against Directoryworks
SELECT distinct [EmpID],[Name Rep]
FROM [AMS_CSSO_BI].[FY18].[tmpAssignmentSubmissions]
where not isnull([EmpID],'!Data issue!') in
(select [employeenumber] from [DirectoryWorks])
-- add missing people to sales employee master. Also needs Sales Role for Anaplan export!
--insert into [FY18].[SalesEmployeeMaster]
([Report Date],[Sales Person],[Employee ID],[Forecast Manager Lv1],[Include],[Geo])
SELECT
getdate()
,[sn]+ ' '+[GivenName]+'('+[EmployeeNumber]+')P1'
,[EmployeeNumber]
-- ,(select top 1 [Manager Name] from [AMS_CSSO_BI].[FY18].[tmpAssignmentSubmissions] where [EmpID]=[EmployeeNumber]) [Manager Name]
,'Carlson Bob(10324243)'
,'True'
,(select top 1 [Geo] from [AMS_CSSO_BI].[FY18].[tmpAssignmentSubmissions] where [EmpID]=[EmployeeNumber]) [geo]
FROM [AMS_CSSO_BI].[dbo].[DirectoryWorks]
where [EmployeeNumber] in (
SELECT distinct [EmpID]
FROM [AMS_CSSO_BI].[FY18].[tmpAssignmentSubmissions]
where not [EmpID] in
(select [employee id] from [FY18].[SalesEmployeeMaster])
)
-- correct manager ID
update [AMS_CSSO_BI].[FY18].[SalesEmployeeMaster]
set [Forecast Manager Lv1]='Pizarro Renee(00332891)P1' --Davis Mary(21343745)P1
where [Forecast Manager Lv1]='Pizarro Renee'
SELECT *
FROM [AMS_CSSO_BI].[FY18].[SalesEmployeeMaster]
where not [Forecast Manager Lv1] like '%(%'
or [Sales Person] like 'Pizarro Renee%'
-- update geo in sales employee master
update [AMS_CSSO_BI].[FY18].[SalesEmployeeMaster]
set [FY18].[SalesEmployeeMaster].Geo=[FY18].[tmpAssignmentSubmissions].Geo
from [FY18].[tmpAssignmentSubmissions]
where [EmpID]=[Employee id]
and not [FY18].[SalesEmployeeMaster].Geo=[FY18].[tmpAssignmentSubmissions].Geo
-- look up anaplan Partner Name for US INDIRECT account
select distinct [FY18 PSA]
,(select COUNT(1) from [FY18].[SalesTerritoryMaster] where [Territory Name] like 'US INDIRECT % '+[FY18 PSA]+'|%') [matches]
,isnull((select top 1 [Territory Name] from [FY18].[SalesTerritoryMaster]
where [Territory Name] like 'US INDIRECT % '+[FY18 PSA]+'|%'
order by [Territory Name] desc),'Need to create Sales Territory in Anaplan') [proposed match]
from [FY18].[tmpAssignmentSubmissions]
where not [FY18 PSA] is null
and [Sales Territory ID] is null
-- fetch ST details for singular US INDIRECT matches
update [FY18].[tmpAssignmentSubmissions]
set [Anaplan Partner Name]=(select top 1 [Territory Name] from [FY18].[SalesTerritoryMaster] where [Territory Name] like 'US INDIRECT % '+[FY18 PSA]+'|%' order by [Territory Name] desc)
,[Sales Territory ID]=(select top 1 [Sales Territory ID] from [FY18].[SalesTerritoryMaster] where [Territory Name] like 'US INDIRECT % '+[FY18 PSA]+'|%' order by [Territory Name] desc)
where not [FY18 PSA] is null
and (select COUNT(1) from [FY18].[SalesTerritoryMaster] where [Territory Name] like 'US INDIRECT % '+[FY18 PSA]+'|%')=1
-- look up Anaplan Partner Name and Sales Territory ID from HQ Location ID
select distinct [seller hq siebel location id]
,[Seller HQ Siebel Company Name]
,(select COUNT(1) from [FY18].[SalesTerritoryMaster] where [Territory Name] like '%'+[seller hq siebel location id]+'|%') [matches]
,isnull((select top 1 [Territory Name] from [FY18].[SalesTerritoryMaster]
where [Territory Name] like '%'+[seller hq siebel location id]+'|%'
order by [Territory Name] DESC ),'Need to create Sales Territory in Anaplan') [proposed match]
,comments
from [FY18].[tmpAssignmentSubmissions]
where not [seller hq siebel location id] is null
and [FY18 PSA] is null
and [Sales Territory ID] is null
-- fetch ST details for singular HQ Location ID matches
update [FY18].[tmpAssignmentSubmissions]
set [Anaplan Partner Name]=(select top 1 [Territory Name] from [FY18].[SalesTerritoryMaster] where [Territory Name] like '%'+[seller hq siebel location id]+'|%' order by [Territory Name] desc)
,[Sales Territory ID]=(select top 1 [Sales Territory ID] from [FY18].[SalesTerritoryMaster] where [Territory Name] like '%'+[seller hq siebel location id]+'|%' order by [Territory Name] desc)
where not [seller hq siebel location id] is null
and [FY18 PSA] is null
and [Sales Territory ID] is null
and (select COUNT(1) from [FY18].[SalesTerritoryMaster] where [Territory Name] like '%'+[seller hq siebel location id]+'|%')=1
-- look up Anaplan Partner Name and Sales Territory ID from HQ Location ID and PSA
select distinct [seller hq siebel location id]
,[Seller HQ Siebel Company Name]
,[FY18 PSA]
,(select COUNT(1) from [FY18].[SalesTerritoryMaster] where [Territory Name] like 'US HQ RPT %'+[seller hq siebel location id]+' '+[fy18 psa]+'|%' and not [Territory Name] like '% SMB %' ) [matches]
,isnull((select top 1 [Territory Name] from [FY18].[SalesTerritoryMaster]
where [Territory Name] like 'US HQ RPT %'+[seller hq siebel location id]+' '+[fy18 psa]+'|%'
and not [Territory Name] like '% SMB %'
order by [Territory Name] desc),'Need to create Sales Territory in Anaplan') [proposed match]
,comments
from [FY18].[tmpAssignmentSubmissions]
where not [seller hq siebel location id] is null
and not [FY18 PSA] is null
and [Sales Territory ID] is null
-- fetch ST details for singular HQ Location ID and PSA matches
update [FY18].[tmpAssignmentSubmissions]
set [Anaplan Partner Name]=(select top 1 [Territory Name] from [FY18].[SalesTerritoryMaster] where [Territory Name] like 'US HQ RPT %'+[seller hq siebel location id]+' '+[fy18 psa]+'|%' and not [Territory Name] like '% SMB %' order by [Territory Name] desc)
,[Sales Territory ID]=(select top 1 [Sales Territory ID] from [FY18].[SalesTerritoryMaster] where [Territory Name] like 'US HQ RPT %'+[seller hq siebel location id]+' '+[fy18 psa]+'|%' and not [Territory Name] like '% SMB %' order by [Territory Name] desc)
where not [seller hq siebel location id] is null
and not [FY18 PSA] is null
and [Sales Territory ID] is null
and (select COUNT(1) from [FY18].[SalesTerritoryMaster] where [Territory Name] like 'US HQ RPT %'+[seller hq siebel location id]+' '+[fy18 psa]+'|%' and not [Territory Name] like '% SMB %' )=1
--data quality check for missing/mis-mapped site location IDs
--INSERT INTO [FY18].[PartnerCrossRef_New] ([Seller Site Siebel Location ID] ,[Seller HQ Siebel Location ID] ,[Seller HQ Siebel Company Name],[Anaplan Partner Name],[Sales Territory ID])
Select distinct [Seller Branch LID],[Seller HQ Siebel Location ID],[Seller HQ Siebel Company Name],[Anaplan Partner Name] from [AMS_CSSO_BI].[FY18].[tmpAssignmentSubmissions]
where not exists
(select 1 from [FY18].[PartnerCrossRef_new] where [Seller Site Siebel Location ID] = [Seller Branch LID] and not [Sales Territory ID] is null)
and not [Name Rep] is null
-- wrong site location ID specified
--SELECT *
--FROM FY17.tmpAssignmentChanges
--WHERE (NOT ([Site Loc Id] LIKE '10______')) AND ([Partner Name] LIKE '%10______|%')
-- data quality check for Anaplan Partner Name field
-- insert into [FY18].[PartnerCrossRef_new] ([Anaplan Partner Name],[Sales Territory ID] )
select distinct isnull([Anaplan Partner Name],'None'),[Sales Territory ID] from [AMS_CSSO_BI].[FY18].[tmpAssignmentSubmissions]
where not isnull([Anaplan Partner Name],'None') in (
select [Anaplan Partner Name] from [FY18].[PartnerCrossRef_new] where not [Anaplan Partner Name] is null
)
-- data quality check for correct and present PSA names for SMB territories. Compare raw rows with result
select [FY18].[tmpAssignmentSubmissions].*
,[PartnerCrossRef_new].[Anaplan Partner Name] [APN]
,[PartnerCrossRef_new].[Sales Territory ID] [STID]
from [AMS_CSSO_BI].[FY18].[tmpAssignmentSubmissions]
left join [FY18].[PartnerCrossRef_new] on ([Seller Site Siebel Location ID] like [Seller Branch LID] + '%'
and [FY18 PSA]=[PSA]
)
where not [FY18 PSA]='N/A'
and [PartnerCrossRef_new].[Anaplan Partner Name] like 'US HQ % SMB %'
-- populate sales territory id for SMB requests
update [FY18].[tmpAssignmentSubmissions]
set [FY18].[tmpAssignmentSubmissions].[sales territory id]=[PartnerCrossRef_new].[Sales Territory ID]
from [FY18].[PartnerCrossRef_new]
where [Seller Site Siebel Location ID] like [Seller Branch LID] + '%'
and [FY18 PSA]=[PSA]
and not [FY18 PSA]='N/A'
and [PartnerCrossRef_new].[Anaplan Partner Name] like 'US HQ % SMB %'
-- data quality check for non-PSA assignments via Anaplan Partner Name
select [FY18].[tmpAssignmentSubmissions].*
,[PartnerCrossRef_new].[Sales Territory ID] [STID]
from [AMS_CSSO_BI].[FY18].[tmpAssignmentSubmissions]
left join [FY18].[PartnerCrossRef_new] on [FY18].[tmpAssignmentSubmissions].[Anaplan Partner Name]=[FY18].[PartnerCrossRef_new].[Anaplan Partner Name]
where [FY18].[tmpAssignmentSubmissions].[sales territory id] is null
-- populate sales territory id from Partner Cross reference via Anaplan Partner Name
update [FY18].[tmpAssignmentSubmissions]
set [FY18].[tmpAssignmentSubmissions].[sales territory id]=[PartnerCrossRef_new].[Sales Territory ID]
from [FY18].[PartnerCrossRef_new]
where [FY18].[tmpAssignmentSubmissions].[Anaplan Partner Name]=[FY18].[PartnerCrossRef_new].[Anaplan Partner Name]
and [FY18].[tmpAssignmentSubmissions].[sales territory id] is null
and [FY18 PSA]='N/A'
-- data quality check for non-PSA assignments via branch location ID
select [FY18].[tmpAssignmentSubmissions].*
,[PartnerCrossRef_new].[Sales Territory ID] [STID]
from [AMS_CSSO_BI].[FY18].[tmpAssignmentSubmissions]
left join [FY18].[PartnerCrossRef_new] on [FY18].[tmpAssignmentSubmissions].[Seller Branch LID]=[FY18].[PartnerCrossRef_new].[Seller Site Siebel Location ID]
where [FY18 PSA]='N/A'
-- populate sales territory id from Partner Cross reference via branch location ID
update [FY18].[tmpAssignmentSubmissions]
set [FY18].[tmpAssignmentSubmissions].[sales territory id]=[PartnerCrossRef_new].[Sales Territory ID]
from [FY18].[PartnerCrossRef_new]
where [FY18].[tmpAssignmentSubmissions].[Seller Branch LID]=[FY18].[PartnerCrossRef_new].[Seller Site Siebel Location ID]
and [FY18 PSA]='N/A'
-- final check
select * from [FY18].[tmpAssignmentSubmissions]
where [sales territory id] is null
-- obsolete previous assignments for submitted reps
-- update script. ONLY RUN ONCE PER FILE!!!
--insert into [FY18].[SalesRepAssignments] ([Partner],[Sales Rep],[Forecast Manager Lv1],[Comp Plan Assigned],[Comp Plan Effective Start Date],[Comp Plan Effective End Date],[Territory Name]
,[Non-QCV Relevant],[Assignment Start Date],[Assignment End Date],[Sales Rep L2],[Sales Territory ID],[Anaplan Territory Code],[Territory to Rep Code],[Action]
,[Requested By],[Requested Date],[Comments])
select distinct [Partner]
,[Sales Rep]
,[Forecast Manager Lv1]
,[Comp Plan Assigned]
,[Comp Plan Effective Start Date]
,[Comp Plan Effective End Date]
,[Territory Name]
,[Non-QCV Relevant]
,[Assignment Start Date]
,[Assignment End Date]
,[Sales Rep L2]
,[FY18].[SalesRepAssignments].[Sales Territory ID]
,[Anaplan Territory Code]
,[Territory to Rep Code]
,[Action]
--,'Cannon Lance(09045981)'
--,'White Won(10127724)'
--,'Seelie Mary(07708007)'
,'Goozen Jill(00545581)'
--,'Davids Diann(07811875)'
--,'Nelson Elizabeth(10152109)'
--,'Tabbert Pat(00803555)'
--,'McClurg Paula(00298903)'
--,'Switzer Andy(10009445)'
[Requested By]
,[date] [Requested Date]
,[Comments]
from [AMS_CSSO_BI].[FY18].[SalesRepAssignments],[FY18].[tmpAssignmentSubmissions]
where [sales rep] like '%('+empid+')P1'
and [Obsoleted by ID] is null
and [action]='Remove'
order by [sales rep]
-- add/remove script. ONLY RUN ONCE PER FILE!!!
--INSERT INTO [FY18].[SalesRepAssignments]
([Sales Rep]
,[Forecast Manager Lv1]
,[Comp Plan Assigned]
,[Territory Name]
,[Sales Territory ID]
,[Action]
,[Requested By]
,[Requested Date]
,[Comments])
select (select [Sales Person] from [FY18].[SalesEmployeeMaster] where [Employee ID]=[Empid])
,(select [Forecast Manager Lv1] from [FY18].[SalesEmployeeMaster] where [Employee ID]=[Empid])
,[Role]
--,(select top 1 [Comp Plan assigned] from [FY17].[SalesRepAssignments] where [Sales Rep] like '%'+[Emp id]+'%' and not [action]='Remove' and [Obsoleted by ID] is null order by [ID] desc)
,(select [Anaplan Partner Name] from [FY18].[PartnerCrossRef_new] where [FY18].[tmpAssignmentSubmissions].[Sales Territory ID] = [FY18].[PartnerCrossRef_new].[Sales Territory ID] )
,[Sales Territory ID]
,[action]
--,'Cannon Lance(09045981)'
--,'White Won(10127724)'
,'Seelie Mary(07708007)'
--,'Goozen Jill(00545581)'
--,'Davids Diann(07811875)'
--,'Nelson Elizabeth(10152109)'
--,'Tabbert Pat(00803555)'
--,'McClurg Paula(00298903)'
--,'Switzer Andy(10009445)'
--,'Walters Cole(21358755)'
,[date]
,[comments]
from [FY18].[tmpAssignmentSubmissions]
where not [Sales Territory ID] is null
-- removes - set prior entries to obsoleted by ID
with [actions] as (
select A.id,b.id [Obsoleted by ID],b.[action],b.[Requested Date] [Obsoleted Date],a.[Sales Rep],a.[Sales Territory ID],a.[Comp Plan Assigned] from [FY18].[SalesRepAssignments] a, [FY18].[SalesRepAssignments] b
where not a.[Action]='Remove'
and a.[Obsoleted by ID] is null
and b.[action]='Remove'
and b.[Obsoleted by ID] is null
and a.[Sales Rep]=b.[sales rep]
and a.[Sales Territory ID]=b.[sales territory id]
--and left(a.[Comp Plan Assigned],4)=left(b.[Comp Plan Assigned],4)
and a.ID<b.ID
)
update [FY18].[SalesRepAssignments] set
[FY18].[SalesRepAssignments].[Obsoleted by ID]=[actions].[obsoleted by id]
,[FY18].[SalesRepAssignments].[Obsoleted date]=[actions].[obsoleted Date]
from [actions]
where [FY18].[SalesRepAssignments].[ID]=[actions].[ID]
---- removal requests- left HPE
--INSERT INTO [FY17].[SalesRepAssignments]
-- ([Sales Rep]
-- ,[Forecast Manager Lv1]
-- ,[Comp Plan Assigned]
-- ,[Territory Name]
-- ,[Sales Territory ID]
-- ,[Action]
-- ,[Requested By]
-- ,[Requested Date]
-- ,[Comments])
--select [Sales Rep]
-- ,(select [Forecast Manager Lv1] from [FY17].[SalesEmployeeMaster] where [Sales Person] =[Sales Rep])
-- ,[Comp Plan Assigned]
-- ,[Territory Name]
-- ,[Sales Territory ID]
-- ,'Remove'
-- --,'Graves Ryan(21794666)'
-- ,'Trexler Elizabeth(10152109)'
-- ,GETDATE()
-- ,'left HPE'
--from [FY17].[SalesRepAssignments]
--where [Sales Rep] like 'Thomas Jeffrey%'
--and not [Action] ='Remove'
--and [Obsoleted by ID] is null
-- FINAL STEP! IMPORTANT!
-- delete from [FY18].[tmpAssignmentsubmissions]
--delete from FY18.SalesRepAssignments where ID in (
--select id from FY18.SalesRepAssignments
----where [action]='Remove'
--where not [Obsoleted by ID] is null
--and not [Territory Name] like '% CDW %'
--)
--update FY18.SalesRepAssignments set [Obsoleted by ID] =null,[Obsoleted Date]= null
--where not [Obsoleted by ID] is null
--and not [Territory Name] like '% CDW %'