-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_objects_mssql.sql
319 lines (296 loc) · 9.22 KB
/
db_objects_mssql.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
drop table if exists dbo.Competition;
go
create table dbo.Competition
(
CompetitionId int
,CompetitionName varchar(50)
,SchoolFullPoints bit
,ScoreByLevel bit
);
go
drop table if exists dbo.Level;
go
create table dbo.Level
(
LevelId int
,LevelName varchar(30)
,Level int
);
go
drop table if exists dbo.School;
go
create table dbo.School
(
SchoolId int
,SchoolName varchar(50)
);
go
drop table if exists dbo.Student;
go
create table dbo.Student
(
StudentId int not null
,SchoolId int
,LastName varchar(20)
,FirstName varchar(15)
,LevelId int
,constraint Student_PK primary key(StudentId)
);
go
drop view if exists dbo.Student_View;
go
--
-- Flat view of Individual Students
--
-- *bj 2019-01-31 created
--
create view Student_View
as
select stu.StudentId
,stu.LastName
,stu.FirstName
,stu.LevelId
,lvl.Level
,lvl.LevelName
,stu.SchoolId
,sch.SchoolName
,Problem = stuff(case when lvl.LevelId = stu.LevelId then '' else ', LevelId' end
+case when sch.SchoolId = stu.SchoolId then '' else ', SchoolId' end
,1,2,'Invalid ')
from Student stu
left join Level lvl on lvl.LevelId = stu.LevelId
left join School sch on sch.SchoolId = stu.SchoolId
;
go
drop table if exists dbo.TestResult;
go
create table dbo.TestResult
(
CompetitionId int
,StudentId int
,Score numeric(4,1)
,Level int
,SrcFile varchar(50)
);
go
drop view if exists dbo.TestResult_View;
go
--
-- Flat view of Individual Student scores for each competition
-- along with their rank within Competition ID + Level ID
--
-- NOTE: Points are awarded based on the top 10 *scores*, not the
-- top 10 students (plus a few extra in case of tie). The top *score*
-- gets 10 points
--
-- *bj 2016-01-28 created
--
create view TestResult_View
as
with data as (
select res.CompetitionId,com.CompetitionName,com.SchoolFullPoints
,res.StudentId ,stu.FirstName ,stu.LastName
,TestLevel = case when com.ScoreByLevel = 1 then res.Level else 0 end
,TestLevelName = lvlTst.LevelName
,StudentLevelId = stu.LevelId
,StudentLevelName = lvlStu.LevelName
,stu.SchoolId ,sch.SchoolName
,res.Score
,StudentRank = rank() over (partition by res.CompetitionId, case when com.ScoreByLevel = 1 then res.Level else 0 end order by res.Score desc)
,ScoreRank = dense_rank() over (partition by res.CompetitionId, case when com.ScoreByLevel = 1 then res.Level else 0 end order by res.Score desc)
,Problem = concat(case when com.CompetitionId is null then 'Unknown Competition; ' end
,case when stu.StudentId / 1000 <> res.Level and res.Level between 1 and 4
or stu.StudentId / 1000 not in (4,5,6) and res.Level = 4
then 'Id / Level Mismatch; ' end
,case when stu.StudentId is null then 'Unknown Student; ' end
,case when lvlTst.LevelId is null and res.Level > 0 then 'Unknown Level; ' end
,case when sch.SchoolId is null then 'Unknown School; ' end
)
from TestResult res
left join Competition com on com.CompetitionId = res.CompetitionId
left join Student stu on stu.StudentId = res.StudentId
left join Level lvlTst on lvlTst.levelId = res.Level
left join Level lvlStu on lvlStu.LevelId = stu.LevelId
left join School sch on sch.SchoolId = stu.SchoolId
)
select *
,StudentScore = case when ScoreRank between 1 and 10 then 11- ScoreRank else 0 end
from data
;
go
drop procedure if exists dbo.Results_Participation;
go
--
-- Show all students for each school along with what events they participated in
--
-- *bj 2016-01-29 created
--
create proc Results_Participation
as
with delegate as (
select sch.SchoolName
,stu.StudentId,stu.LastName,stu.FirstName
,lvl.LevelName
from Student stu
join School sch on sch.SchoolId = stu.SchoolId
left join Level lvl on lvl.LevelId = stu.LevelId
where coalesce(stu.LevelId,1) between 1 and 6
)
select del.SchoolName
,del.LevelName
,del.LastName,del.FirstName
,CompetitionName = coalesce(res.CompetitionName,'<Participated in No Events>')
,Score = coalesce(res.Score,0)
from delegate del
left join TestResult_View res on res.StudentId = del.StudentId
order by del.SchoolName
,del.LevelName
,del.LastName,del.FirstName
,res.CompetitionId
;
go
drop procedure if exists dbo.Results_QualQuant;
go
--
-- 1st Place Qual = Highest Avg Per Delegate of all schools
-- 1st Place Quant = Highest Total Points of remaining schools
-- 2nd Place Qual = Highest Avg Per Delegate of remaining schools
-- 2nd Place Quant = Highest Total Points of remaining schools
-- etc
--
-- *bj 2016-01-29 created
--
create proc Results_QualQuant
as
set nocount on;
with SchoolFullPoints as (
select SchoolId ,Points = sum(StudentScore) from TestResult_View where SchoolFullPoints = 1 group by SchoolId
), SchoolPartialByLevel as (
select SchoolId,StudentLevelId,Points = avg(StudentScore) from TestResult_View where SchoolFullPoints = 0 group by SchoolId,StudentLevelId
), SchoolPartialPoints as (
select SchoolId ,Points = sum(Points ) from SchoolPartialByLevel group by SchoolId
), SchoolTotalPoints as (
select ful.SchoolId
,Points = coalesce(ful.Points,0) + coalesce(prt.Points,0)
from SchoolFullPoints ful
full join SchoolPartialPoints prt on prt.SchoolId = ful.SchoolId
), SchoolDelegates as (
select SchoolId
,NumDelegates = count(1)
from Student
where LevelId between 1 and 6
group by SchoolId
)
select sch.SchoolName
,del.NumDelegates
,pts.Points
,AvgPerDelegate = cast(pts.Points*1.0/del.NumDelegates as numeric(6,2))
,QualPlace = cast(null as int)
,QuantPlace = cast(null as int)
into #results
from SchoolDelegates del
join SchoolTotalPoints pts on pts.SchoolId = del.SchoolId
join School sch on sch.SchoolId = del.SchoolId
;
declare @Place int = 1
,@SchoolId int
;
while exists (select 1 from #results where QualPlace is null and QuantPlace is null)
begin
with qual as (
select top 1* from #results
where QualPlace is null and QuantPlace is null
order by AvgPerDelegate desc
)
update qual set QualPlace = @Place
;
with quant as (
select top 1* from #results
where QualPlace is null and QuantPlace is null
order by Points desc
)
update quant set QuantPlace = @Place
;
set @Place = @Place + 1;
end;
select *
from #results
order by QualPlace desc,QuantPlace desc
;
go
drop procedure if exists dbo.Results_Top10;
go
--
-- Dump out formatted-ish top 10 list by competition and level
--
-- *bj 2016-01-29 created
--
create proc Results_Top10
as
select *
,Place = cast(ScoreRank as varchar(5))
,Points = cast(StudentScore as varchar(5))
,CompLevel = case when TestLevel = -1 then 'All' else 'Level ' + rtrim(TestLevel) end
,Grp1 = CompetitionId
,Grp2 = TestLevel
,Grp3 = cast(9 as int)
into #results
from TestResult_View
where StudentScore > 0
;
alter table #results drop column Problem;
insert into #results (Grp1,Grp2,Grp3)
select distinct Grp1,Grp2,0 from #results; -- blank space between competitions
insert into #results (Grp1,Grp2,Grp3)
select distinct Grp1,Grp2,0 from #results; -- blank space between competitions
insert into #results (Grp1,Grp2,Grp3,CompetitionName)
select distinct Grp1,Grp2,1,CompetitionName from #results where CompetitionName is not null; -- Competition Header
insert into #results (Grp1,Grp2,Grp3)
select distinct Grp1,Grp2,2 from #results; -- blank space between levels
insert into #results (Grp1,Grp2,Grp3,CompLevel)
select distinct Grp1,Grp2,3,CompLevel from #results where CompLevel is not null; -- Level header
update #results
set CompetitionName = coalesce(CompetitionName,'')
,CompLevel = coalesce(CompLevel ,'')
,Place = coalesce(Place ,'')
,Points = coalesce(Points ,'')
,SchoolName = coalesce(SchoolName ,'')
,FirstName = coalesce(FirstName ,'')
,LastName = coalesce(LastName ,'')
,StudentLevelName= coalesce(StudentLevelName,'')
select Competition = case when Grp3 = 1 then CompetitionName else '' end
,Level = case when Grp3 = 3 then CompLevel else '' end
,Place
,Points
,SchoolName,FirstName,LastName
,StudentLevelName
from #results
order by Grp1,Grp2,Grp3
,StudentScore
;
go
drop procedure if exists dbo.Results_TopOverall;
go
--
-- Show top 10 students overall
--
-- *bj 2016-01-29 created
--
create proc Results_TopOverall
@Places int = 10
as
with data as (
select Place = row_number() over (order by sum(StudentScore) desc)
,Points = sum(StudentScore)
,SchoolName
,FirstName,LastName
,StudentLevel = StudentLevelName
from TestResult_View res
group by SchoolName,FirstName,LastName,StudentLevelName
)
select *
from data
where Place <= @Places
order by Place desc
;
go