-
Notifications
You must be signed in to change notification settings - Fork 2
/
SQL031.sql
226 lines (210 loc) · 7.83 KB
/
SQL031.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
/*
PLT058 - Script para Boletim Individual
Create By Bitts
(23/06/2017)
Para utilização em RELATÓRIOS
Upgrade:
- 22/06/2017
- Utilização de Variaveis
- definição de novo parametro para exibir notas de acordo com o Trimestre passado
*/
DECLARE @CODCOLIGADA INT = '3';
DECLARE @CODFILIAL INT = '3';
DECLARE @RA VARCHAR(10) = '0004923';
DECLARE @IDPERLET VARCHAR(5) = '57';
DECLARE @IDHABILITACAOFILIAL INT = 82;
DECLARE @TRIMESTRE INT = 2;
SELECT
A.CODFILIAL,
F.NOME AS ALUNO,
A.RA AS RA,
I.NOME AS CURSO,
A.CODTURMA AS TURMA,
D.NUMDIARIO AS NUMCHAMADA,
L.NOME AS DISCIPLINA,
Q.CH AS CARGA_HORARIA,
C.CODPERLET AS PLETIVO,
P.NOME AS SERIE,
A.CODSTATUSRES,
C.DIASLETIVOS,
N1 = CONVERT(VARCHAR, (CASE WHEN @TRIMESTRE >= 1 THEN ISNULL(CONVERT(VARCHAR, N1.NOTA),'-') ELSE '' END)),
F1 = CONVERT(VARCHAR, (CASE WHEN @TRIMESTRE >= 1 THEN ISNULL(CONVERT(VARCHAR, F1.FALTAS),'0') ELSE '' END)),
N2 = CONVERT(VARCHAR, (CASE WHEN @TRIMESTRE >= 2 THEN ISNULL(CONVERT(VARCHAR, N2.NOTA),'-') ELSE '' END)),
F2 = CONVERT(VARCHAR, (CASE WHEN @TRIMESTRE >= 2 THEN ISNULL(CONVERT(VARCHAR, F2.FALTAS),'0') ELSE '' END)),
N3 = CONVERT(VARCHAR, (CASE WHEN @TRIMESTRE >= 3 THEN ISNULL(CONVERT(VARCHAR, N3.NOTA),'-') ELSE '' END)),
F3 = CONVERT(VARCHAR, (CASE WHEN @TRIMESTRE >= 3 THEN ISNULL(CONVERT(VARCHAR, F3.FALTAS),'0') ELSE '' END)),
MA = CONVERT(VARCHAR, (CASE WHEN @TRIMESTRE >= 3 THEN ISNULL(CONVERT(VARCHAR, MA.NOTA),'-') ELSE '' END)),
EX = CONVERT(VARCHAR, (CASE WHEN @TRIMESTRE >= 3 THEN ISNULL(CONVERT(VARCHAR, EX.NOTA),'-') ELSE '' END)),
NF = CONVERT(VARCHAR, (CASE WHEN @TRIMESTRE >= 3 THEN ISNULL(CONVERT(VARCHAR, NF.NOTA),'-') ELSE '' END)),
MEDIAANUAL = (
CASE
WHEN N1.NOTA IS NOT NULL AND N2.NOTA IS NOT NULL AND N3.NOTA IS NOT NULL AND ((N1.NOTA + N2.NOTA + N3.NOTA)/3) >= 7
THEN (N1.NOTA + N2.NOTA + N3.NOTA)/3
WHEN N1.NOTA IS NOT NULL AND N2.NOTA IS NOT NULL AND N3.NOTA IS NOT NULL AND ((N1.NOTA + N2.NOTA + N3.NOTA)/3) < 7 AND EX.NOTA IS NOT NULL
THEN ((N1.NOTA + N2.NOTA + N3.NOTA)/3)
ELSE NULL END),
AULAS.TOTAL AS TOTALAULAS,
SITUACAO = (CASE WHEN N.DESCRICAO = 'Matriculado' THEN '-' ELSE N.DESCRICAO END),
RESFINAL = (CASE WHEN O.DESCRICAO IS NULL THEN ''ELSE O.DESCRICAO END)
FROM
SMATRICPL AS A (NOLOCK)
INNER JOIN GFILIAL AS B (NOLOCK) ON
A.CODCOLIGADA = B.CODCOLIGADA AND
A.CODFILIAL = B.CODFILIAL
INNER JOIN SPLETIVO AS C (NOLOCK) ON
A.CODCOLIGADA = C.CODCOLIGADA AND
A.IDPERLET = C.IDPERLET
INNER JOIN SMATRICULA AS D (NOLOCK) ON
A.CODCOLIGADA = D.CODCOLIGADA AND
A.IDPERLET = D.IDPERLET AND
A.RA = D.RA AND
A.IDHABILITACAOFILIAL = D.IDHABILITACAOFILIAL
INNER JOIN SALUNO AS E (NOLOCK) ON
D.CODCOLIGADA = E.CODCOLIGADA AND
E.CODTIPOCURSO = C.CODTIPOCURSO AND
D.RA = E.RA
INNER JOIN PPESSOA AS F (NOLOCK) ON
E.CODPESSOA = F.CODIGO
INNER JOIN SHABILITACAOFILIAL AS G (NOLOCK) ON
A.CODCOLIGADA = G.CODCOLIGADA AND
A.IDHABILITACAOFILIAL = G.IDHABILITACAOFILIAL AND
A.CODFILIAL = G.CODFILIAL
INNER JOIN STURNO AS H (NOLOCK) ON
H.CODCOLIGADA = G.CODCOLIGADA AND
H.CODTURNO = G.CODTURNO AND
H.CODFILIAL = G.CODFILIAL AND
H.CODTIPOCURSO = C.CODTIPOCURSO
LEFT JOIN SCURSO I (NOLOCK) ON
I.CODCOLIGADA = G.CODCOLIGADA AND
I.CODCURSO = G.CODCURSO
INNER JOIN SHABILITACAO AS J (NOLOCK) ON
J.CODCOLIGADA = G.CODCOLIGADA AND
J.CODCURSO = G.CODCURSO AND
J.CODHABILITACAO = G.CODHABILITACAO
INNER JOIN STURMADISC AS K (NOLOCK) ON
K.CODCOLIGADA = D.CODCOLIGADA AND
K.IDTURMADISC = D.IDTURMADISC
INNER JOIN SDISCIPLINA AS L (NOLOCK) ON
L.CODCOLIGADA = K.CODCOLIGADA AND
L.CODDISC = K.CODDISC
INNER JOIN SSTATUS AS N (NOLOCK) ON
N.CODCOLIGADA = D.CODCOLIGADA AND
N.CODSTATUS = D.CODSTATUS AND
N.CODTIPOCURSO = C.CODTIPOCURSO
LEFT JOIN SSTATUS AS O (NOLOCK) ON
O.CODCOLIGADA = A.CODCOLIGADA AND
O.CODSTATUS = A.CODSTATUSRES AND
O.CODTIPOCURSO = C.CODTIPOCURSO
LEFT JOIN SHABILITACAO AS P (NOLOCK) ON
P.CODCURSO = G.CODCURSO AND
P.CODHABILITACAO = G.CODHABILITACAO
LEFT JOIN SDISCGRADE AS Q (NOLOCK) ON
Q.CODDISC = L.CODDISC AND
Q.CODHABILITACAO = P.CODHABILITACAO AND
Q.CODCURSO = I.CODCURSO AND
Q.CODCOLIGADA = A.CODCOLIGADA AND
Q.CODGRADE = G.CODGRADE
LEFT JOIN SPERIODO AS R (NOLOCK) ON
R.CODCURSO = Q.CODCURSO AND
R.CODHABILITACAO = P.CODHABILITACAO AND
R.CODGRADE = Q.CODGRADE AND
R.CODPERIODO = Q.CODPERIODO
LEFT JOIN STURMA AS S (NOLOCK) ON
S.CODCOLIGADA = A.CODCOLIGADA AND
S.CODFILIAL = A.CODFILIAL AND
S.IDPERLET = A.IDPERLET AND
S.CODTURMA = A.CODTURMA
LEFT JOIN (
SELECT RA, CONVERT(FLOAT, NOTAFALTA) AS NOTA, CODCOLIGADA, IDTURMADISC, CODETAPA
FROM SNOTAETAPA
) N1 (RA, NOTA, CODCOLIGADA, IDTURMADISC, CODETAPA) ON
N1.CODCOLIGADA = D.CODCOLIGADA AND
N1.IDTURMADISC = D.IDTURMADISC AND
N1.RA = D.RA AND
N1.CODETAPA = '5'
LEFT JOIN (
SELECT RA, CONVERT(FLOAT, NOTAFALTA) AS FALTAS, CODCOLIGADA, IDTURMADISC, CODETAPA
FROM SNOTAETAPA
) F1 (RA, FALTAS, CODCOLIGADA, IDTURMADISC, CODETAPA) ON
F1.CODCOLIGADA = D.CODCOLIGADA AND
F1.IDTURMADISC = D.IDTURMADISC AND
F1.RA = D.RA AND
F1.CODETAPA = '6'
LEFT JOIN (
SELECT RA, CONVERT(FLOAT, NOTAFALTA) AS NOTA, CODCOLIGADA, IDTURMADISC, CODETAPA
FROM SNOTAETAPA
) N2 (RA, NOTA, CODCOLIGADA, IDTURMADISC, CODETAPA) ON
N2.CODCOLIGADA = D.CODCOLIGADA AND
N2.IDTURMADISC = D.IDTURMADISC AND
N2.RA = D.RA AND
N2.CODETAPA = '11'
LEFT JOIN (
SELECT RA, CONVERT(FLOAT, NOTAFALTA) AS FALTAS, CODCOLIGADA, IDTURMADISC, CODETAPA
FROM SNOTAETAPA
) F2 (RA, FALTAS, CODCOLIGADA, IDTURMADISC, CODETAPA) ON
F2.CODCOLIGADA = D.CODCOLIGADA AND
F2.IDTURMADISC = D.IDTURMADISC AND
F2.RA = D.RA AND
F2.CODETAPA = '12'
LEFT JOIN (
SELECT RA, CONVERT(FLOAT, NOTAFALTA) AS NOTA, CODCOLIGADA, IDTURMADISC, CODETAPA
FROM SNOTAETAPA
) N3 (RA, NOTA, CODCOLIGADA, IDTURMADISC, CODETAPA) ON
N3.CODCOLIGADA = D.CODCOLIGADA AND
N3.IDTURMADISC = D.IDTURMADISC AND
N3.RA = D.RA AND
N3.CODETAPA = '17'
LEFT JOIN (
SELECT RA, CONVERT(FLOAT, NOTAFALTA) AS FALTAS, CODCOLIGADA, IDTURMADISC, CODETAPA
FROM SNOTAETAPA
) F3 (RA, FALTAS, CODCOLIGADA, IDTURMADISC, CODETAPA) ON
F3.CODCOLIGADA = D.CODCOLIGADA AND
F3.IDTURMADISC = D.IDTURMADISC AND
F3.RA = D.RA AND
F3.CODETAPA = '18'
LEFT JOIN (
SELECT RA, CONVERT(FLOAT, NOTAFALTA) AS NOTA, CODCOLIGADA, IDTURMADISC, CODETAPA
FROM SNOTAETAPA
) MA (RA, NOTA, CODCOLIGADA, IDTURMADISC, CODETAPA) ON
MA.CODCOLIGADA = D.CODCOLIGADA AND
MA.IDTURMADISC = D.IDTURMADISC AND
MA.RA = D.RA AND
MA.CODETAPA = '19'
LEFT JOIN (
SELECT RA, CONVERT(FLOAT, NOTAFALTA) AS NOTA, CODCOLIGADA, IDTURMADISC, CODETAPA
FROM SNOTAETAPA
) EX (RA, NOTA, CODCOLIGADA, IDTURMADISC, CODETAPA) ON
EX.CODCOLIGADA = D.CODCOLIGADA AND
EX.IDTURMADISC = D.IDTURMADISC AND
EX.RA = D.RA AND
EX.CODETAPA = '20'
LEFT JOIN (
SELECT RA, CONVERT(FLOAT, NOTAFALTA) AS NOTA, CODCOLIGADA, IDTURMADISC, CODETAPA
FROM SNOTAETAPA
) NF (RA, NOTA, CODCOLIGADA, IDTURMADISC, CODETAPA) ON
NF.CODCOLIGADA = D.CODCOLIGADA AND
NF.IDTURMADISC = D.IDTURMADISC AND
NF.RA = D.RA AND
NF.CODETAPA = '21'
LEFT JOIN (
SELECT RA, CONVERT(INT, AULASDADAS) AS TOTAL, CODCOLIGADA, IDTURMADISC, CODETAPA
FROM SNOTAETAPA X
) AULAS (RA, TOTAL, CODCOLIGADA, IDTURMADISC, CODETAPA) ON
AULAS.CODCOLIGADA = D.CODCOLIGADA AND
AULAS.IDTURMADISC = D.IDTURMADISC AND
AULAS.RA = D.RA AND
AULAS.CODETAPA = '9'
WHERE
S.CODCOLIGADA = @CODCOLIGADA AND
C.IDPERLET = @IDPERLET AND
A.RA = @RA AND
G.IDHABILITACAOFILIAL = @IDHABILITACAOFILIAL AND
C.CODFILIAL = @CODFILIAL AND
A.CODSTATUS IN ('17','43','54','64','67','47','23','53','39','50','60','2','44','65','55','19','18','20','34','28','51','40','61') AND
D.NUMDIARIO IS NOT NULL
ORDER BY
Q.POSHIST,
I.CODCURSO,
A.CODTURMA,
A.RA,
L.CODDISC;