-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStandardC.sql
975 lines (891 loc) · 32.1 KB
/
StandardC.sql
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
DECLARE
@p_StartDate date
, @p_EndDate date
, @p_stateList varchar(10) = 'B823CFDA-A383-E911-80D7-0A253F89019C'
, @p_zipcodeList varchar(9) = '74464'
, @p_county varchar(120)-- not found in CRM data entry in person, prospect pages
, @p_cityname varchar(120) = 'Tah' -- dnu/ too expensive
, @p_veteran varchar(1)-- not found in CRM data entry
, @p_household_ind varchar(1) -- APRXREF_HOUSEHOLD_IND -- flag on xref rec linking people at same address
-- at same address and other than married, why exclude?
-- at same address and married/partnered then primary spouse only flag is same condition
, @p_include_deceased varchar(1) = 'Y' -- y/n
, @p_primary_spouse_only varchar(1) = 'Y' -- y/n
, @p_gift_capacity varchar(99)
, @p_wealth_engine_des varchar(1)
, @p_donor_cats varchar(99) -- aldc / alumni degree completion, alum - degreed slumna/us
, @p_exclusion_codes varchar(3) -- ams, nak
, @p_mail_codes varchar(99) -- ack - acknowledgements/reminders, acl -alumni/club chapter mailings
, @p_special_purpose_types varchar(99) -- nsueg - nsu employee giving design, nsuin - nsu support interest
, @p_special_purpose_groups varchar(99) -- acaff - academic affaris, admn - administration
, @p_activities varchar(99) -- adplc - president's leadership class,
, @p_activity_years varchar(4) -- list of years
, @p_leadership_roles varchar(99) -- stvlead
, @p_academic_years varchar(4) -- list of years
, @p_majors varchar(99) -- 0000 - undeclared, 1100 - business admin
, @p_degrees varchar(99) -- a - associates, aa - associates in arts
;
SELECT
e.elcn_PersonID,
e.elcn_educationid,
d.elcn_code DEGREE,
d.elcn_Name AS Degree_Name,
e.elcn_DegreeYear Degree_Year,
(
Select TOP 1 mb.elcn_name FROM elcn_education_elcn_majorBase emb
INNER JOIN elcn_majorBase mb ON mb.elcn_majorId = emb.elcn_majorid
Where emb.elcn_educationid = e.elcn_educationId
) AS Major,
ROW_NUMBER() OVER(PARTITION BY e.elcn_personId
ORDER BY e.elcn_DegreeYear DESC) AS RANK_NO
INTO #temp_education
FROM dbo.elcn_educationBase e
JOIN elcn_degreeBase d on d.elcn_degreeId = e.elcn_DegreeId
WHERE e.statuscode = 1
;
CREATE NONCLUSTERED INDEX INDX_TMP_EDUCATION_RANKS ON #temp_education (elcn_personId);
SELECT
elcn_personid,
CASE elcn_typeid
WHEN '1172C46B-462D-E411-9415-005056804B43' THEN 'CIFE'
WHEN '0F72C46B-462D-E411-9415-005056804B43' THEN 'SIFE'
WHEN '1B72C46B-462D-E411-9415-005056804B43' THEN 'SIFL'
WHEN '89799F16-C4E8-4269-B409-5756998F193F' THEN 'CIFL'
ELSE cast(elcn_typeid as varchar(40))
END AS SALU_CODE,
elcn_formattedname,
elcn_locked,
modifiedon
INTO
#temp_aprsalu
FROM
elcn_formattednamebase
WHERE
elcn_typeid in ('1172C46B-462D-E411-9415-005056804B43', -- Joint Mailing Name (CIFE)
'0F72C46B-462D-E411-9415-005056804B43', --Mailing Name (SIFE)
'1B72C46B-462D-E411-9415-005056804B43', --Casual Salutation (SIFL)
'89799F16-C4E8-4269-B409-5756998F193F') --Casual Joint Saluation (CIFL)
AND (elcn_enddate >= SYSDATETIME()
OR elcn_enddate IS NULL)
;
CREATE NONCLUSTERED INDEX INDX_TMP_ID_SALU ON #temp_aprsalu (elcn_personId,salu_code);
SELECT DISTINCT
elcn_person personid,
datepart(YYYY,elcn_ContributionDate)givingyear,
datepart(YYYY,elcn_ContributionDate) -1 prevyear
INTO
#temp_dontations
FROM
elcn_contributiondonorBase
;
--CREATE NONCLUSTERED INDEX INDX_TMP_ID_YEAR ON #temp_donations (personId,givingyear desc);
SELECT elcn_personid, elcn_ratingtypeid, [Value], [Level], [Score]
INTO #temp_ratings
FROM(
SELECT
elcn_ratingBase.elcn_ratingDescription ,
elcn_ratingBase.elcn_personid,
elcn_ratingBase.elcn_ratingtypeid,
elcn_ratingBase.elcn_ratingvalue
FROM
elcn_ratingBase
) T PIVOT
(
MAX(elcn_ratingvalue)
FOR elcn_ratingDescription IN
([Value], [Level], [Score])
) PVT
;
CREATE NONCLUSTERED INDEX INDX_TMP_ID_RATINGTYPE ON #temp_ratings (elcn_personid, elcn_ratingtypeid);
SELECT
elcn_PrimaryMemberPersonId,
elcn_membershipprogramlevelbase.elcn_name ,
elcn_statusbase.elcn_name status,
elcn_membershipBase.elcn_MembershipNumber , --7701
CONVERT(DATE, elcn_membershipBase.elcn_ExpireDate) elcn_ExpireDate -- null
INTO #temp_membership
FROM
elcn_membershipBase
JOIN elcn_membershipprogramlevelbase
ON elcn_membershipBase.elcn_MembershipLevelId = elcn_membershipprogramlevelid
JOIN elcn_statusbase
ON elcn_membershipBase.elcn_MembershipStatusId = elcn_statusid
;
CREATE NONCLUSTERED INDEX INDX_TMP_ID_MEMBERSHIP ON #temp_membership (elcn_primarymemberpersonid);
SELECT
elcn_personid,
Preferred,
Business,
Personal,
Other,
Yellow,
Alumni,
NSU
INTO
#temp_email_slot
FROM(
SELECT
case elcn_typeid
WHEN 'CC0141A1-A383-E911-80D7-0A253F89019C' THEN 'Business'
WHEN 'CD0141A1-A383-E911-80D7-0A253F89019C' THEN 'Personal'
WHEN '31292157-E075-4E85-9204-1CCEDEC9DBF9' THEN 'Other'
WHEN 'F523FC9B-5370-46F3-9242-263411E73043' THEN 'Yellow ' -- Waiting Authorization
WHEN 'A4B7A0CC-0DFF-4069-8337-6571B94CA5BD' THEN 'Alumni'
WHEN '26E175F1-4286-4B7E-9CE9-F2E383D58EFD' THEN 'NSU'
ELSE 'Unknown'
END AS emailtype,
elcn_personid,
elcn_email
FROM
elcn_emailaddressbase
WHERE
statuscode = 1
AND elcn_EmailAddressStatusId = '378DE114-EB09-E511-943C-0050568068B7' -- current
-- 050FE7CB-5508-E511-943C-0050568068B7 -- past
UNION ALL
SELECT
'Preferred',
elcn_personid,
elcn_email
FROM
elcn_emailaddressbase
WHERE
statuscode =1
AND elcn_EmailAddressStatusId = '378DE114-EB09-E511-943C-0050568068B7' -- current
AND elcn_preferred = 1
)T PIVOT
( MAX(elcn_email)
FOR emailtype
IN ([Preferred], [Business], [Personal], [Other], [Yellow], [Alumni], [NSU])
)PVT;
CREATE NONCLUSTERED INDEX INDX_TMP_ID ON #temp_email_slot (elcn_personid);
-->> Phone
SELECT
elcn_personid, --guid
elcn_phonenumber, --phone number
elcn_type,
elcn_preferred -- 0/1
INTO #temp_phone
FROM(
SELECT
elcn_personid
, elcn_phonenumber
, elcn_phonetypeBase.elcn_type
, elcn_preferred
, ROW_NUMBER() OVER (PARTITION BY elcn_personid, elcn_phonetypebase.elcn_type
ORDER BY elcn_preferred DESC) RN
FROM
elcn_phonebase
JOIN elcn_phonetypebase
ON elcn_phonebase.elcn_phonetype = elcn_phonetypebase.elcn_phonetypeid
AND elcn_phonebase.elcn_phonestatusid = '378DE114-EB09-E511-943C-0050568068B7' -- Current
AND elcn_phonebase.statuscode = 1
) PHONES
WHERE
RN = 1
CREATE NONCLUSTERED INDEX INDX_TMP_ID ON #temp_phone (elcn_personid);
with w_get_consec_years AS (
--anchor
select personid,
givingyear,
prevyear,
1 consecyears
from #temp_dontations
--recusive memeber
union all
select d.personid,
d.givingyear,
d.prevyear,
cte.consecyears +1 consecyears
from #temp_dontations d
inner join w_get_consec_years cte
on cte.personid = d.personid
and d.givingyear -1 = cte.givingyear
--termination
),
w_get_longest_consec_years AS (
--anchor
select personid,
givingyear,
prevyear,
1 consecyears
from #temp_dontations
--recusive memeber
union all
select d.personid,
d.givingyear,
d.prevyear,
cte.consecyears +1 consecyears
from #temp_dontations d
inner join w_get_consec_years cte
on cte.personid = d.personid
and d.givingyear -1 = cte.givingyear
--termination
)
SELECT
cb.ContactId,
CASE
WHEN cb.elcn_dateofdeath IS NULL THEN 'N' -- not dead
ELSE 'Y'
END AS DECEASED_IND,
cb.elcn_dateofbirth DATE_OF_BIRTH,
cb.elcn_PrimaryID pidm,
cb.datatel_EnterpriseSystemId ID,
cb.fullname as Primary_Name, -- NAME
( --- Brad's - not in standard c proc. Keep?
SELECT TOP 1
pnb.elcn_firstname
FROM elcn_personnameBase pnb
WHERE pnb.elcn_nametype = 'EBC22907-A5CB-4270-8947-C5381D1ECC54' /*Preferred First Name*/
AND pnb.elcn_EndDate IS NULL
AND pnb.statuscode =1
AND pnb.elcn_personid = cb.contactid
ORDER BY pnb.CreatedOn
) AS Preferred_First_Name,
cb.lastname as Last_Name, -- PREF_LAST_NAME
(
SELECT TOP 1
pnb.elcn_lastname
FROM elcn_personnameBase pnb
WHERE pnb.elcn_nametype = '29C69522-08C1-48E3-A030-F417A0E741C0' /*Maiden Name*/
AND pnb.elcn_EndDate IS NULL
AND pnb.statuscode = 1
AND pnb.elcn_personid = cb.contactid
ORDER BY pnb.CreatedOn
) AS MAIDEN_LAST_NAME,
COALESCE(cife_salu.elcn_formattedname,sife_salu.elcn_formattedname) PREFERRED_FULL_W_SALUTATION,
COALESCE(cifl_salu.elcn_formattedname,sifl_salu.elcn_formattedname) PREFERRED_SHORT_W_SALUTATION,
sife_salu.elcn_formattedname SIFE,
sifl_salu.elcn_formattedname SIFL,
ctb.value Primary_Constituent_Type, -- PREF_DONOR_CATEGORY
ctb.elcn_type AS Primary_Constituent_Desc, -- PREF_DONOR_CATEGORY_DESC
ab.elcn_street1 AS Street_Line1,
ab.elcn_street2 AS Street_Line2,
ab.elcn_City AS City,
spb.elcn_Abbreviation as State_Province,
ab.elcn_postalcode AS Postal_Code,
ab.elcn_county County,
dcb.Datatel_name as Nation,
atb.elcn_type AS Preferred_Address_Type, --ADDRESS_TYPE
-- Exclusion categories in ATVEXCL
-- NPH - No Phone
(CASE WHEN(
SELECT COUNT(*) FROM elcn_contactpreferenceBase cpb
WHERE cpb.elcn_ContactPreferenceTypeId = '112A7585-A2D9-E911-80D8-0A253F89019C' /*Communications*/
AND cpb.elcn_ContactRestrictionId = '8872A718-5472-40C4-82C7-DB72FC4CE5A6' /*Exclude*/
AND (cpb.elcn_RestrictionLiftDate < CURRENT_TIMESTAMP OR cpb.elcn_RestrictionLiftDate IS NULL)
AND cpb.elcn_ContactPreferenceStatusId = '378DE114-EB09-E511-943C-0050568068B7' /*Current*/
AND cpb.elcn_MethodofContact = 344220001 /*Phone*/
AND cpb.elcn_personId = cb.ContactId
) > 0 THEN 'NPH' ELSE NULL END) AS NPH,
-- NOC - No Contact
(CASE WHEN(
SELECT COUNT(*) FROM elcn_contactpreferenceBase cpb
WHERE cpb.elcn_ContactPreferenceTypeId = '76EA8AA5-2F36-4E8E-BFB2-490677DCF4B4' /*Global Restriction*/
AND cpb.elcn_ContactRestrictionId = '8872A718-5472-40C4-82C7-DB72FC4CE5A6' /*Exclude*/
AND (cpb.elcn_RestrictionLiftDate < CURRENT_TIMESTAMP OR cpb.elcn_RestrictionLiftDate IS NULL)
AND cpb.elcn_ContactPreferenceStatusId = '378DE114-EB09-E511-943C-0050568068B7' /*Current*/
AND cpb.elcn_MethodofContact = 344220006 /*All*/
AND cpb.elcn_personId = cb.ContactId
) > 0 THEN 'NOC' ELSE NULL END) AS NOC,
-- NMC - No Mail Contact
(CASE WHEN(
SELECT COUNT(*) FROM elcn_contactpreferenceBase cpb
WHERE cpb.elcn_ContactPreferenceTypeId = '112A7585-A2D9-E911-80D8-0A253F89019C' /*Communications*/
AND cpb.elcn_ContactRestrictionId = '8872A718-5472-40C4-82C7-DB72FC4CE5A6' /*Exclude*/
AND (cpb.elcn_RestrictionLiftDate < CURRENT_TIMESTAMP OR cpb.elcn_RestrictionLiftDate IS NULL)
AND cpb.elcn_ContactPreferenceStatusId = '378DE114-EB09-E511-943C-0050568068B7' /*Current*/
AND cpb.elcn_MethodofContact = 344220000 /*Letter*/
AND cpb.elcn_personId = cb.ContactId
) > 0 THEN 'NMC' ELSE NULL END) AS NMC,
-- NEM - No E-mail
(CASE WHEN(
SELECT COUNT(*) FROM elcn_contactpreferenceBase cpb
WHERE cpb.elcn_ContactPreferenceTypeId = '112A7585-A2D9-E911-80D8-0A253F89019C' /*Communications*/
AND cpb.elcn_ContactRestrictionId = '8872A718-5472-40C4-82C7-DB72FC4CE5A6' /*Exclude*/
AND (cpb.elcn_RestrictionLiftDate < CURRENT_TIMESTAMP OR cpb.elcn_RestrictionLiftDate IS NULL)
AND cpb.elcn_ContactPreferenceStatusId = '378DE114-EB09-E511-943C-0050568068B7' /*Current*/
AND cpb.elcn_MethodofContact = 344220002 /*Email*/
AND cpb.elcn_personId = cb.ContactId
) > 0 THEN 'NEM' ELSE NULL END) AS NEM,
-- NAM - No Alumni Association Mailings
(CASE WHEN(
SELECT COUNT(*) FROM elcn_contactpreferenceBase cpb
WHERE cpb.elcn_ContactPreferenceTypeId = 'EE8CE7BD-9CB8-E911-80D8-0A253F89019C' /*Alumni / Club Chapter Mailings*/
AND cpb.elcn_ContactRestrictionId = '8872A718-5472-40C4-82C7-DB72FC4CE5A6' /*Exclude*/
AND (cpb.elcn_RestrictionLiftDate < CURRENT_TIMESTAMP OR cpb.elcn_RestrictionLiftDate IS NULL)
AND cpb.elcn_ContactPreferenceStatusId = '378DE114-EB09-E511-943C-0050568068B7' /*Current*/
AND cpb.elcn_MethodofContact = 344220000 /*Letter*/
AND cpb.elcn_personId = cb.ContactId
) > 0 THEN 'NAM' ELSE NULL END) AS NAM,
-- NDN - No Donation Solicitations
(CASE WHEN(
SELECT COUNT(*) FROM elcn_contactpreferenceBase cpb
WHERE cpb.elcn_ContactPreferenceTypeId = 'e4e02dc6-3314-e511-9431-005056804b43' /*Solicitations*/
AND cpb.elcn_ContactRestrictionId = '8872A718-5472-40C4-82C7-DB72FC4CE5A6' /*Exclude*/
AND (cpb.elcn_RestrictionLiftDate < CURRENT_TIMESTAMP OR cpb.elcn_RestrictionLiftDate IS NULL)
AND cpb.elcn_ContactPreferenceStatusId = '378DE114-EB09-E511-943C-0050568068B7' /*Current*/
AND cpb.elcn_MethodofContact = 344220006 /*All*/
AND cpb.elcn_personId = cb.ContactId
) > 0 THEN 'NDN' ELSE NULL END) AS NDN,
-- NAK - No Acknowledgement Letters
(CASE WHEN(
SELECT COUNT(*) FROM elcn_contactpreferenceBase cpb
WHERE cpb.elcn_ContactPreferenceTypeId = 'DEE02DC6-3314-E511-9431-005056804B43' /*Acknowledgements*/
AND cpb.elcn_ContactRestrictionId = '8872A718-5472-40C4-82C7-DB72FC4CE5A6' /*Exclude*/
AND (cpb.elcn_RestrictionLiftDate < CURRENT_TIMESTAMP OR cpb.elcn_RestrictionLiftDate IS NULL)
AND cpb.elcn_ContactPreferenceStatusId = '378DE114-EB09-E511-943C-0050568068B7' /*Current*/
AND cpb.elcn_MethodofContact = 344220000 /*Letter*/
AND cpb.elcn_personId = cb.ContactId
) > 0 THEN 'NAK' ELSE NULL END) AS NAK,
-- NTP - No Third Party Solicitations
(CASE WHEN(
SELECT COUNT(*) FROM elcn_contactpreferenceBase cpb
WHERE cpb.elcn_ContactPreferenceTypeId = 'e4e02dc6-3314-e511-9431-005056804b43' /*Solicitations*/
AND cpb.elcn_ContactRestrictionId = '8872A718-5472-40C4-82C7-DB72FC4CE5A6' /*Exclude*/
AND (cpb.elcn_RestrictionLiftDate < CURRENT_TIMESTAMP OR cpb.elcn_RestrictionLiftDate IS NULL)
AND cpb.elcn_ContactPreferenceStatusId = '378DE114-EB09-E511-943C-0050568068B7' /*Current*/
AND cpb.elcn_MethodofContact = 344220006 /*All*/
AND cpb.elcn_personId = cb.ContactId
) > 0 THEN 'NTP' ELSE NULL END) AS NTP,
cb.anonymityType Anonymity_Type,
--->> RATINGS
jfsg_est_cap.value JFSG_Estimated_Capacity,
ratings1.rating_type RATING_TYPE1,
ratings1.rating_score RATING_AMOUNT1,
ratings1.rating_value RATING1,
ratings1.rating_level RATING_LEVEL1,
ratings2.rating_type RATING_TYPE2,
ratings2.rating_score RATING_AMOUNT2,
ratings2.rating_value RATING2,
ratings2.rating_level RATING_LEVEL2,
null RATING_TYPE3,
null RATING_AMOUNT3,
null RATING3,
null RATING_LEVEL3,
(
SELECT
COUNT(elcn_RecognitionCredit)
FROM
elcn_contributiondonorBase
JOIN elcn_contribution
ON elcn_contributiondonorBase.elcn_contribution = elcn_contribution.elcn_contributionId
WHERE
elcn_contributiondonorBase.elcn_person = cb.ContactId
AND elcn_contribution.statuscode = 1
AND elcn_contribution.elcn_contributionType IN (344220000, -- Gift
344220001, -- Pledge
344220004, -- Matching Gift
344220005) -- Bequest Expectancy
and elcn_contributiondonorBase.elcn_ContributionDate BETWEEN @p_StartDate AND @p_EndDate
) Lifetime_Number_of_Gifts, --TOTAL_NO_GIFTS
cb.elcn_LargestContributionAmount Largest_Contribution_Amount, -- HIGH_GIFT_AMT
CONVERT(VARCHAR,cb.elcn_LastContributionDate,101) Last_Contibution_Date, -- LAST_GIFT_DATE 12/30/2006 format
(
SELECT
SUM(elcn_RecognitionCredit)
FROM
elcn_contributiondonorBase
JOIN elcn_contribution
ON elcn_contributiondonorBase.elcn_contribution = elcn_contribution.elcn_contributionId
WHERE
elcn_contributiondonorBase.elcn_person = cb.ContactId
AND elcn_contribution.statuscode = 1
AND elcn_contribution.elcn_contributionType IN (344220000, -- Gift
344220001, -- Pledge
344220004, -- Matching Gift
344220005) -- Bequest Expectancy
and datepart(YYYY,elcn_contributiondonorBase.elcn_ContributionDate) = datepart(YYYY,sysdatetime())
) Gifts_YTD,
(
SELECT
SUM(elcn_RecognitionCredit)
FROM
elcn_contributiondonorBase
JOIN elcn_contribution
ON elcn_contributiondonorBase.elcn_contribution = elcn_contribution.elcn_contributionId
WHERE
elcn_contributiondonorBase.elcn_person = cb.ContactId
AND elcn_contribution.statuscode = 1
AND elcn_contribution.elcn_contributionType IN (344220000, -- Gift
344220001, -- Pledge
344220004, -- Matching Gift
344220005) -- Bequest Expectancy
and datepart(YYYY,elcn_contributiondonorBase.elcn_ContributionDate) = datepart(YYYY,sysdatetime()) -1
) Gifts_Year2,
(
SELECT
SUM(elcn_RecognitionCredit)
FROM
elcn_contributiondonorBase
JOIN elcn_contribution
ON elcn_contributiondonorBase.elcn_contribution = elcn_contribution.elcn_contributionId
WHERE
elcn_contributiondonorBase.elcn_person = cb.ContactId
AND elcn_contribution.statuscode = 1
AND elcn_contribution.elcn_contributionType IN (344220000, -- Gift
344220001, -- Pledge
344220004, -- Matching Gift
344220005) -- Bequest Expectancy
and datepart(YYYY,elcn_contributiondonorBase.elcn_ContributionDate) = datepart(YYYY,sysdatetime()) -2
) Gifts_Year3,
(
SELECT
SUM(elcn_RecognitionCredit)
FROM
elcn_contributiondonorBase
JOIN elcn_contribution
ON elcn_contributiondonorBase.elcn_contribution = elcn_contribution.elcn_contributionId
WHERE
elcn_contributiondonorBase.elcn_person = cb.ContactId
AND elcn_contribution.statuscode = 1
AND elcn_contribution.elcn_contributionType IN (344220000, -- Gift
344220001, -- Pledge
344220004, -- Matching Gift
344220005) -- Bequest Expectancy
and datepart(YYYY,elcn_contributiondonorBase.elcn_ContributionDate) = datepart(YYYY,sysdatetime()) -3
) Gifts_Year4,
longest_consec.longest_consec_years LONGEST_CONS_YEARS_GIVEN,
consec.consecyears RECENT_CONSECUTIVE_YEARS,
gen_membership.elcn_name MEMBERSHIP_NAME,
gen_membership.status MEMBERSHIP_STATUS,
gen_membership.elcn_membershipnumber MEMBERSHIP_NUMBER,
gen_membership.elcn_expiredate EXPIRATION_DATE,
won_membership.elcn_name WON_MEMBERSHIP_NAME,
won_membership.status WON_MEMBERSHIP_STATUS,
won_membership.elcn_membershipnumber WON_MEMBERSHIP_NUMBER,
won_membership.elcn_expiredate WON_EXPIRATION_DATE,
fan_membership.elcn_name FAN_MEMBERSHIP_NAME,
fan_membership.status FAN_MEMBERSHIP_STATUS,
fan_membership.elcn_membershipnumber FAN_MEMBERSHIP_NUMBER,
fan_membership.elcn_expiredate FAN_EXPIRATION_DATE,
eab.elcn_name EMAIL_PREFERRED_ADDRESS,
email_slot.personal PERS_EMAIL,
email_slot.nsu NSU_EMAIL,
email_slot.alumni AL_EMAIL,
email_slot.business BUS_EMAIL,
homephone.elcn_phonenumber Home_Phome, --PR_PHONE_NUMBER
CASE homephone.elcn_preferred
WHEN 1 then 'Y'
ELSE null
END Home_Phone_Preferred, --PR_PRIMARY_IND
cellphone.elcn_phonenumber CL_PHONE_NUMBER,
CASE cellphone.elcn_preferred
WHEN 1 then 'Y'
ELSE null
END Cell_Preferred, --CL_PRIMARY_IND
busphone.elcn_phonenumber Business_Phone, --B1_PHONE_NUMBER
CASE busphone.elcn_preferred
WHEN 1 then 'Y'
ELSE null
END Business_Phone_Preferred, --B1_PRIMARY_IND
--TOTAL_PLEDGE_PAYMENTS1
(
SELECT
SUM(elcn_contributionBase.elcn_Amount)
FROM
elcn_contribution --PAYMENT
JOIN elcn_contributiondonorBase
ON elcn_contribution.elcn_contributionId = elcn_contributiondonorBase.elcn_contribution
JOIN elcn_contributionBase --PLEDGE
ON elcn_contribution.elcn_PaymentforContribution = elcn_contributionBase.elcn_contributionId
WHERE
elcn_contributiondonorBase.elcn_person = cb.ContactId
AND elcn_contributionBase.statuscode = 1
AND elcn_contribution.elcn_contributionType = 344220003 -- Pledge Payment
AND elcn_contributiondonorBase.elcn_AssociationTypeId = '36FA0E30-6248-E411-941F-0050568068B8' -- Primary only, If incl Spouse, would need to include Group/Joint
) Lifetime_Pledge_Payments, --Total Pledge Payments
(
SELECT
SUM(elcn_contributionBase.elcn_Amount)
FROM
elcn_contribution --PAYMENT
JOIN elcn_contributiondonorBase
ON elcn_contribution.elcn_contributionId = elcn_contributiondonorBase.elcn_contribution
JOIN elcn_contributionBase --PLEDGE
ON elcn_contribution.elcn_PaymentforContribution = elcn_contributionBase.elcn_contributionId
WHERE
elcn_contributiondonorBase.elcn_person = cb.ContactId
AND elcn_contributionBase.statuscode = 1
AND elcn_contribution.elcn_contributionType = 344220003 -- Pledge Payment
AND elcn_contributiondonorBase.elcn_ContributionDate BETWEEN @p_StartDate AND @p_EndDate
AND elcn_contributiondonorBase.elcn_AssociationTypeId = '36FA0E30-6248-E411-941F-0050568068B8' -- Primary only, If incl Spouse, would need to include Group/Joint
) Total_Pledge_Payments, --Total Pledge Payments
-- LIFE_TOTAL_GIVING
cb.elcn_totalGiving Lifetime_Giving,
-- TOTAL_GIVING1
(
SELECT
SUM(elcn_RecognitionCredit)
FROM
elcn_contributiondonorBase
JOIN elcn_contribution
ON elcn_contributiondonorBase.elcn_contribution = elcn_contribution.elcn_contributionId
WHERE
elcn_contributiondonorBase.elcn_person = cb.ContactId
AND elcn_contribution.statuscode = 1
AND elcn_contribution.elcn_contributionType IN (344220000, -- Gift
344220001, -- Pledge
344220004, -- Matching Gift
344220005) -- Bequest Expectancy
and elcn_contributiondonorBase.elcn_ContributionDate BETWEEN @p_StartDate AND @p_EndDate
) Total_Giving,
(
SELECT
SUM(elcn_contribution.elcn_TotalPremiumFairMarketValue)
FROM
elcn_contributiondonorBase
JOIN elcn_contribution
ON elcn_contributiondonorBase.elcn_contribution = elcn_contribution.elcn_contributionId
WHERE
elcn_contributiondonorbase.elcn_person = cb.ContactId
AND elcn_contribution.statuscode = 1
AND elcn_contribution.elcn_contributionType IN (344220000, -- Gift
344220001, -- Pledge
344220004, -- Matching Gift
344220005) -- Bequest Expectancy
) Lifetime_Premiums,
-- LIFE_TOTAL_GIVING_AUX -- total of fair market value of all contributions except dues payments
(
SELECT
SUM(elcn_contribution.elcn_TotalPremiumFairMarketValue)
FROM
elcn_contributiondonorBase
JOIN elcn_contribution
ON elcn_contributiondonorBase.elcn_contribution = elcn_contribution.elcn_contributionId
WHERE
elcn_contributiondonorbase.elcn_person = cb.ContactId
AND elcn_contribution.statuscode = 1
AND elcn_contribution.elcn_contributionType IN (344220000, -- Gift
344220001, -- Pledge
344220004, -- Matching Gift
344220005) -- Bequest Expectancy
and elcn_contributiondonorBase.elcn_ContributionDate BETWEEN @p_StartDate AND @p_EndDate
) Total_Premiums,
(
SELECT
SUM(elcn_contribution.elcn_marketValue)
FROM
elcn_contributiondonorBase
JOIN elcn_contribution
ON elcn_contributiondonorBase.elcn_contribution = elcn_contribution.elcn_contributionId
WHERE
elcn_contributiondonorBase.elcn_person = cb.ContactId
AND elcn_contribution.statuscode = 1
AND elcn_contribution.elcn_contributionType IN (344220000, -- Gift
344220001, -- Pledge
344220004, -- Matching Gift
344220005) -- Bequest Expectancy
) Lifetime_Fair_Market_Value,--Total Lifetime Fair Market Value
(
SELECT
SUM(elcn_contribution.elcn_marketValue)
FROM
elcn_contributiondonorBase
JOIN elcn_contribution
ON elcn_contributiondonorBase.elcn_contribution = elcn_contribution.elcn_contributionId
WHERE
elcn_contributiondonorBase.elcn_person = cb.ContactId
AND elcn_contribution.statuscode = 1
AND elcn_contribution.elcn_contributionType IN (344220000, -- Gift
344220001, -- Pledge
344220004, -- Matching Gift
344220005) -- Bequest Expectancy
and elcn_contributiondonorBase.elcn_ContributionDate BETWEEN @p_StartDate AND @p_EndDate
) Total_Fair_Market_Value, --Total Fair Market Value (For given date range)
/**************************
ANNUAL_HOUSEHOLD_GIVING -- Householding tools in CRM 3.0
LIFETIME_HOUSEHOLD_GIVING -- Householding tools in CRM 3.0
**************************/
--RELATION_SOURCE_DESC
prt.elcn_type Relationship,
spouse_p.fullname Relation_Name,
--COMBINED_MAILING_PRIORITY
CASE spouse_link.elcn_jointmailing
WHEN 1 THEN 'Y'
ELSE null
END Joint_Mailing,
edu_1.Degree Degree1_Degree,
edu_1.Degree_Name AS Degree1_Degree_Desc,
edu_1.Major AS Degree1_Major,
edu_1.Degree_Year Degree1_Degree_Year,
edu_2.Degree Degree2_Degree,
edu_2.Degree_Name AS Degree2_Degree_Desc,
edu_2.Major AS Degree2_Major,
edu_2.Degree_Year Degree2_Degree_Year,
edu_3.Degree Degree3_Degree,
edu_3.Degree_Name AS Degree3_Degree_Desc,
edu_3.Major AS Degree3_Major,
edu_3.Degree_Year Degree3_Degree_Year,
elcn_OrganizationIdName EMPLOYER,
elcn_JobTitle POSITION,
elcn_BusinessRelationshipStatusIdName Status,
activities.alist ACTIVITIES
--;select *
FROM(
SELECT
contactbase.*,
elcn_anonymitytypeBase.elcn_type anonymityType
FROM
ContactBase
LEFT JOIN elcn_anonymitytypebase
ON contactbase.elcn_AnonymityTypeId = elcn_anonymitytypeBase.elcn_anonymitytypeId
WHERE
fullname not like '%DO%NOT%USE'
AND (@p_include_deceased = 'Y'
OR elcn_dateofdeath is null)
) cb
JOIN elcn_addressassociationBase aab
ON aab.elcn_personId = cb.ContactId
AND aab.elcn_Preferred =1
JOIN elcn_addressBase AB
ON ab.elcn_addressId = aab.elcn_AddressId
AND LEFT(ab.elcn_postalcode,5) IN (@p_zipcodeList)
JOIN elcn_stateprovinceBase spb
ON spb.elcn_stateprovinceId = ab.elcn_StateProvinceId
--AND spb.elcn_stateprovinceId IN (@p_stateList)
JOIN Datatel_countryBase dcb
ON dcb.Datatel_countryId = ab.elcn_country
JOIN elcn_addresstypeBase atb
ON atb.elcn_addresstypeId = aab.elcn_AddressTypeId
LEFT JOIN elcn_personalrelationshipBase SPOUSE_LINK -- includes elcn_jointmailing
ON spouse_link.elcn_Person1Id = cb.ContactId
AND spouse_link.elcn_RelationshipType1ID IN ( '42295D4F-A6EE-E411-942F-005056804B43' , /*Spouse*/
'4F665855-A3B8-E911-80D8-0A253F89019C', /*Spouse / Partner*/
'62295D4F-A6EE-E411-942F-005056804B43', /*Domestic Partner*/
'43665855-A3B8-E911-80D8-0A253F89019C') /*Life Partner*/
AND (spouse_link.elcn_EndDate is null
OR spouse_link.elcn_EndDate > GETDATE())
AND spouse_link.statuscode = 1
LEFT JOIN elcn_personalrelationshiptype prt
ON spouse_link.elcn_RelationshipType1Id = prt.elcn_personalrelationshiptypeid
LEFT JOIN ContactBase spouse_p
ON spouse_p.ContactId = spouse_link.elcn_Person2Id
LEFT JOIN elcn_constituentaffiliationBase cab ON cab.elcn_constituentaffiliationId = cb.elcn_primaryconstituentaffiliationid
LEFT JOIN(
SELECT
elcn_constituenttypeBase.*,
filteredstringmap.value
FROM
elcn_constituenttypeBase
JOIN filteredstringmap
ON elcn_constituenttypeBase.elcn_category = filteredstringmap.attributevalue
AND FilteredViewName = 'Filteredelcn_constituenttype'
AND attributeName = 'elcn_category'
) ctb ON ctb.elcn_constituenttypeID = cab.elcn_ConstituentTypeId
LEFT JOIN(
SELECT
elcn_personid,
elcn_formattedname,
ROW_NUMBER() OVER (PARTITION BY elcn_personid ORDER BY elcn_locked DESC, modifiedon DESC) RN
FROM
#temp_aprsalu salu
WHERE
salu.salu_code = 'CIFE'
) CIFE_SALU
ON cb.contactid = cife_salu.elcn_personid
AND cife_salu.rn = 1
LEFT JOIN(
SELECT
elcn_personid,
elcn_formattedname,
ROW_NUMBER() OVER (PARTITION BY elcn_personid ORDER BY elcn_locked DESC, modifiedon DESC) RN
FROM
#temp_aprsalu salu
WHERE
salu.salu_code = 'SIFE'
) SIFE_SALU
ON cb.contactid = sife_salu.elcn_personid
AND sife_salu.rn = 1
LEFT JOIN(
SELECT
elcn_personid,
elcn_formattedname,
ROW_NUMBER() OVER (PARTITION BY elcn_personid ORDER BY elcn_locked DESC, modifiedon DESC) RN
FROM
#temp_aprsalu salu
WHERE
salu.salu_code = 'CIFL'
) CIFL_SALU
ON cb.contactid = cifl_salu.elcn_personid
AND cifl_salu.rn = 1
LEFT JOIN(
SELECT
elcn_personid,
elcn_formattedname,
ROW_NUMBER() OVER (PARTITION BY elcn_personid ORDER BY elcn_locked DESC, modifiedon DESC) RN
FROM
#temp_aprsalu salu
WHERE
salu.salu_code = 'SIFL'
) SIFL_SALU
ON cb.contactid = sifl_salu.elcn_personid
AND sifl_salu.rn = 1
LEFT JOIN #temp_education edu_1 on edu_1.elcn_PersonId = cb.ContactID and edu_1.rank_no = 1
LEFT JOIN #temp_education edu_2 on edu_2.elcn_PersonId = cb.ContactID and edu_2.rank_no = 2
LEFT JOIN #temp_education edu_3 on edu_3.elcn_PersonId = cb.ContactID and edu_3.rank_no = 3
LEFT JOIN(
SELECT
personid,
MAX(consecyears) consecyears
FROM
w_get_consec_years
GROUP BY personid
)CONSEC ON cb.contactid = consec.personid
LEFT JOIN(
SELECT
personid,
MAX(consecyears) LONGEST_CONSEC_YEARS
FROM
w_get_longest_consec_years
GROUP BY personid
)LONGEST_CONSEC on cb.contactid = longest_consec.personid
LEFT JOIN(
SELECT
tr.elcn_personid
, tr.Value
FROM
#temp_ratings TR
WHERE
elcn_ratingtypeid = '88C5BD4A-BF21-4635-B8BB-EBE956F2E5BD' -- JFSG Est Capacity-DonorSearch
) JFSG_EST_CAP ON cb.ContactId = jfsg_est_cap.elcn_personid
LEFT JOIN(
SELECT
tr.elcn_personid
, 'JF Smith Group Top 500' RATING_TYPE
, tr.value RATING_VALUE
, tr.level RATING_LEVEL
, tr.score RATING_SCORE
FROM
#temp_ratings TR
WHERE
elcn_ratingtypeid = '1BB294D5-53D7-4815-8963-096802773E6D' -- JF Smith Group Top 500
)RATINGS1 ON cb.contactid = ratings1.elcn_personid
LEFT JOIN(
SELECT
tr.elcn_personid
, 'iWave Pro Score' RATING_TYPE
, tr.value RATING_VALUE
, tr.level RATING_LEVEL
, tr.score RATING_SCORE
FROM
#temp_ratings TR
WHERE
elcn_ratingtypeid = '3DE9ACBB-37E5-45AF-8902-2314FC2A9538' -- iWave Pro Score
)RATINGS2 ON cb.contactid = ratings2.elcn_personid
LEFT JOIN( -- general membership
SELECT
elcn_PrimaryMemberPersonId,
elcn_name,
status,
elcn_membershipnumber,
elcn_expiredate,
ROW_NUMBER() OVER (PARTITION BY elcn_PrimaryMemberPersonId
ORDER BY ISNULL(elcn_expiredate,'31-DEC-2999') DESC) rn
FROM
#temp_membership
WHERE
( elcn_name not like 'FAN%'
AND elcn_name not like 'WON%'
)
) GEN_MEMBERSHIP ON cb.contactid = gen_membership.elcn_PrimaryMemberPersonId
AND gen_membership.rn = 1
LEFT JOIN( -- Future Alumni Network (FAN) membership
SELECT
elcn_PrimaryMemberPersonId,
elcn_name,
status,
elcn_membershipnumber,
elcn_expiredate,
ROW_NUMBER() OVER (PARTITION BY elcn_PrimaryMemberPersonId
ORDER BY ISNULL(elcn_expiredate,'31-DEC-2999') DESC) rn
FROM
#temp_membership
WHERE
elcn_name like 'FAN%'
) FAN_MEMBERSHIP ON cb.contactid = fan_membership.elcn_PrimaryMemberPersonId
AND fan_membership.rn = 1
LEFT JOIN( -- Women of Northeastern (WON) membership
SELECT
elcn_PrimaryMemberPersonId,
elcn_name,
status,
elcn_membershipnumber,
elcn_expiredate,
ROW_NUMBER() OVER (PARTITION BY elcn_PrimaryMemberPersonId
ORDER BY ISNULL(elcn_expiredate,'31-DEC-2999') DESC) rn
FROM
#temp_membership
WHERE
elcn_name like 'WON%'
) WON_MEMBERSHIP ON cb.contactid = won_membership.elcn_PrimaryMemberPersonId
AND won_membership.rn = 1
LEFT JOIN elcn_emailaddressbase eab
ON eab.elcn_personid = cb.contactid
AND eab.elcn_preferred = 1
LEFT JOIN #temp_email_slot email_slot
ON cb.contactid = email_slot.elcn_personid
LEFT JOIN(
SELECT
elcn_personid,
elcn_phonenumber,
elcn_type , -- 'Home' etc
elcn_preferred -- 0/1
FROM
#temp_phone
WHERE
elcn_type = 'Home'
)HOMEPHONE ON cb.contactid = homephone.elcn_personid
LEFT JOIN(
SELECT
elcn_personid,
elcn_phonenumber,
elcn_type ,
elcn_preferred
FROM
#temp_phone
WHERE
elcn_type = 'Cell'
)CELLPHONE ON cb.contactid = cellphone.elcn_personid
LEFT JOIN(
SELECT
elcn_personid,
elcn_phonenumber,
elcn_type ,
elcn_preferred
FROM
#temp_phone
WHERE
elcn_type = 'Business'
)BUSPHONE ON cb.contactid = busphone.elcn_personid
LEFT JOIN(
SELECT
elcn_personid,
elcn_JobTitle,
elcn_OrganizationIdName,
elcn_BusinessRelationshipStatusIdName
FROM
elcn_businessrelationship
WHERE
elcn_PrimaryEmployer = 1
AND statuscode =1
)JOB ON cb.contactid = job.elcn_personid
LEFT JOIN(
SELECT
elcn_personid,
STRING_AGG(ib.elcn_name + ' (' + sb.elcn_name + ')', ';') AS ALIST
FROM
elcn_involvementBase ib
JOIN elcn_statusbase sb
ON ib.elcn_InvolvementStatusId = sb.elcn_statusid
GROUP BY elcn_personid
)ACTIVITIES ON cb.contactid = activities.elcn_personid
WHERE
cb.statuscode =1
AND--elcn_personalrelationshipbase prb
(@p_primary_spouse_only = 'N' -- include everyone
OR (spouse_link.elcn_person1id IS NOT NULL -- they have a relationship
AND cb.contactid = spouse_link.elcn_primaryspouseid) --and this person is primary
)
and cb.FullName like '%Mutzig%'
;