-
Notifications
You must be signed in to change notification settings - Fork 0
/
CadenceSelContacts.sql
148 lines (137 loc) · 3.68 KB
/
CadenceSelContacts.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
USE [Campus6]
GO
/****** Object: StoredProcedure [custom].[CadenceSelContacts] Script Date: 2021-01-05 14:56:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Wyatt Best
-- Create date: 2020-10-23
-- Description: Selects three terms' worth of students and various fields to send to Mongoose Cadence.
-- =============================================
CREATE PROCEDURE [custom].[CadenceSelContacts] @Dept NVARCHAR(2)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @AcademicYear NVARCHAR(4) = (
SELECT dbo.fnGetAbtSetting('ACA_RECORDS', 'CURRENT_YT', 'CURRENT_YEAR')
)
,@AcademicTerm NVARCHAR(10) = (
SELECT dbo.fnGetAbtSetting('ACA_RECORDS', 'CURRENT_YT', 'CURRENT_TERM')
)
,@TermId INT
,@SPTermId INT
,@SUTermId INT
,@FATermId INT
SELECT @TermId = TermId
FROM [custom].vwOrderedTerms OT
WHERE ACADEMIC_YEAR = @AcademicYear
AND ACADEMIC_TERM = @AcademicTerm
--Find Spring, Summer, and Fall terms
SELECT @SPTermId = CASE ACADEMIC_TERM
WHEN 'SPRING'
THEN @TermId
WHEN 'SUMMER'
THEN @TermId - 1
WHEN 'FALL'
THEN @TermId + 1
END
,@SUTermId = CASE ACADEMIC_TERM
WHEN 'SPRING'
THEN @TermId + 1
WHEN 'SUMMER'
THEN @TermId
WHEN 'FALL'
THEN @TermId - 1
END
,@FATermId = CASE ACADEMIC_TERM
WHEN 'SPRING'
THEN @TermId - 1
WHEN 'SUMMER'
THEN @TermId + 1
WHEN 'FALL'
THEN @TermId
END
FROM [custom].vwOrderedTerms OT
WHERE TermId = @TermId
--Select list of students
SELECT DISTINCT PEOPLE_CODE_ID
INTO #Students
FROM [custom].vwACADEMIC A
WHERE TermId BETWEEN @TermId - 1 AND @TermId + 2
AND ACADEMIC_FLAG = 'Y'
AND [STATUS] IN (
'A'
,'G'
)
AND ACADEMIC_SESSION > ''
--Add in columns
SELECT S.PEOPLE_CODE_ID [uniqueCampusId]
,dbo.fnPeopleOrgName(S.PEOPLE_CODE_ID, 'FN') [firstName]
,dbo.fnPeopleOrgName(S.PEOPLE_CODE_ID, 'LN') [lastName]
,Phone.*
,Enrollment.SHORT_DESC [Enrollment]
,try_cast(SP_Credits.CREDITS AS INT) [SP_Credits]
,try_cast(SU_Credits.CREDITS AS INT) [SU_Credits]
,try_cast(FA_Credits.CREDITS AS INT) [FA_Credits]
,CASE T.[STATUS]
WHEN 'A'
THEN 0
WHEN 'I'
THEN 1
ELSE NULL
END AS [optedOut]
FROM #Students S
OUTER APPLY (
SELECT TOP 1 CASE
WHEN LEFT(PP.PhoneNumber, 1) = '1' --US country code already prepended
THEN PP.PhoneNumber
ELSE '1' + PP.PhoneNumber
END AS [mobileNumber]
FROM PEOPLE P
INNER JOIN PersonPhone PP
ON PP.PersonId = P.PersonId
AND DoNotCallReason IS NULL
AND PhoneType = 'MOBILE1'
WHERE P.PEOPLE_CODE_ID = S.PEOPLE_CODE_ID
AND CountryId = 240 --US numbers only
ORDER BY CASE
WHEN P.PrimaryPhoneId = PP.PersonPhoneId
THEN GETDATE()
ELSE PP.Revision_Date + PP.Revision_Time
END DESC
) AS Phone
OUTER APPLY (
SELECT TOP 1 SHORT_DESC
FROM [custom].vwACADEMIC A
INNER JOIN CODE_ENROLLMENT
ON CODE_VALUE_KEY = ENROLL_SEPARATION
WHERE A.PEOPLE_CODE_ID = S.PEOPLE_CODE_ID
AND TermId BETWEEN @TermId - 1 AND @TermId + 2
ORDER BY A.TERMID DESC
) Enrollment
OUTER APPLY (
SELECT COALESCE(SUM(CREDITS), 0) [CREDITS]
FROM [custom].vwACADEMIC A
WHERE A.PEOPLE_CODE_ID = S.PEOPLE_CODE_ID
AND A.TermId = @SPTermId
) AS SP_Credits
OUTER APPLY (
SELECT COALESCE(SUM(CREDITS), 0) [CREDITS]
FROM [custom].vwACADEMIC A
WHERE A.PEOPLE_CODE_ID = S.PEOPLE_CODE_ID
AND A.TermId = @SUTermId
) AS SU_Credits
OUTER APPLY (
SELECT COALESCE(SUM(CREDITS), 0) [CREDITS]
FROM [custom].vwACADEMIC A
WHERE A.PEOPLE_CODE_ID = S.PEOPLE_CODE_ID
AND A.TermId = @FATermId
) AS FA_Credits
LEFT JOIN TELECOMMUNICATIONS T
ON T.PEOPLE_ORG_CODE_ID = S.PEOPLE_CODE_ID
AND T.COM_TYPE = 'SMS' + @Dept
DROP TABLE #Students
END
GO