-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariables and Functions.sql
518 lines (382 loc) · 9.73 KB
/
Variables and Functions.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
-- Functions
use Library
-- Write a function that returns a list of books with the minimum number of pages issued by a particular publisher
CREATE OR ALTER FUNCTION function_1_1(@publishername as nvarchar(50))
RETURNS nvarchar (50)
AS
BEGIN
declare @bookname as nvarchar(50)
Select
@bookname =Library.dbo.Books.Name
from
Library.dbo.Books
Inner Join Library.dbo.Press
ON
Library.dbo.Books.Id_Press=Library.dbo.Press.Id
where
Library.dbo.Press.Name=@publishername
AND
Library.dbo.Books.Pages=
(
Select
Min(Pages)
from
Library.dbo.Books
Inner Join Library.dbo.Press
ON
Library.dbo.Books.Id_Press=Library.dbo.Press.Id
where
Library.dbo.Press.Name=@publishername
)
RETURN @bookname
END
declare @result_1_1 nvarchar(50)
exec @result_1 = function_1 N'Piter'
print @result
CREATE OR ALTER FUNCTION function_1_2(@publishername as nvarchar(50))
RETURNS TABLE
AS
RETURN
(
Select
Library.dbo.Books.Pages,
Library.dbo.Books.[Name] as Book,
Library.dbo.Press.[Name] as Pulblisher
from
Library.dbo.Books
Inner Join Library.dbo.Press
ON
Library.dbo.Books.Id_Press=Library.dbo.Press.Id
where
Library.dbo.Press.Name=@publishername
AND
Library.dbo.Books.Pages=
(
Select
Min(Pages)
from
Library.dbo.Books
Inner Join Library.dbo.Press
ON
Library.dbo.Books.Id_Press=Library.dbo.Press.Id
where
Library.dbo.Press.Name=@publishername
)
)
select * from function_1_2(N'Piter')
-- Write a function that returns the names of publishers who have published books with an average number of pages greater than N. The average number of pages is passed through the parameter.
CREATE OR ALTER FUNCTION function_2_1(@AVGpagegreatenthanN as int)
RETURNS nvarchar (50)
AS
BEGIN
declare @publishername as nvarchar(50)
Select
@publishername = Library.dbo.Press.[Name]
from
Library.dbo.Books
Inner Join Library.dbo.Press
ON
Library.dbo.Books.Id_Press=Library.dbo.Press.Id
Group by
Library.dbo.Press.Id,
Library.dbo.Press.[Name]
Having
AVG(Pages)>@AVGpagegreatenthanN
RETURN @publishername
End
declare @result_2 nvarchar(50)
exec @result_2 = function_2_1 100
print @result_2
CREATE OR ALTER FUNCTION function_2_2(@AVGpagegreatenthanN as int)
RETURNS TABLE
AS
RETURN
(
Select
AVG(Pages) AS [AVG(Pages)>N],
Library.dbo.Press.[Name] as Pulblisher
from
Library.dbo.Books
Inner Join Library.dbo.Press
ON
Library.dbo.Books.Id_Press=Library.dbo.Press.Id
Group by
Library.dbo.Press.Id,
Library.dbo.Press.[Name]
Having
AVG(Pages)>@AVGpagegreatenthanN
)
select * from function_2_2(100)
-- Write a function that returns the total sum of the pages of all the books in the library issued by the specified publisher.
CREATE OR ALTER FUNCTION function_3_1(@publisher as nvarchar(50))
RETURNS bigint
AS
BEGIN
declare @sumpages as bigint=0
Select
@sumpages = SUM(Pages)
from
Library.dbo.Books
Inner Join Library.dbo.Press
ON
Library.dbo.Books.Id_Press=Library.dbo.Press.Id
where
Library.dbo.Press.Name=@publisher
RETURN @sumpages
END
declare @result_3 bigint
exec @result_3=function_3_1 N'Binom'
print @result_3
CREATE OR ALTER FUNCTION function_3_2(@publisher as nvarchar(50))
RETURNS TABLE
AS
RETURN
(
Select
SUM(Pages) as [Sum of Pages]
from
Library.dbo.Books
Inner Join Library.dbo.Press
ON
Library.dbo.Books.Id_Press=Library.dbo.Press.Id
where
Library.dbo.Press.[Name]=@publisher
)
select * from function_3_2(N'Binom')
-- Write a function that returns a list of names and surnames of all students who took books between the two specified dates
CREATE OR ALTER FUNCTION function_4(@datein as nvarchar(50), @dateout as nvarchar(50))
RETURNS TABLE
AS
RETURN
(
Select
Library.dbo.S_Cards.DateIn,
Library.dbo.S_Cards.DateOut,
Library.dbo.Students.FirstName,
Library.dbo.Students.LastName
from
Library.dbo.Students
Inner Join Library.dbo.S_Cards
ON
Library.dbo.Students.Id=Library.dbo.S_Cards.Id_Student
Inner Join Library.dbo.Books
ON
Library.dbo.Books.Id=Library.dbo.S_Cards.Id_Book
Group by
Library.dbo.S_Cards.DateIn,
Library.dbo.S_Cards.DateOut,
Library.dbo.Students.FirstName,
Library.dbo.Students.LastName
Having
Library.dbo.S_Cards.DateIn >=@datein
AND
Library.dbo.S_Cards.DateIn<=@dateout
)
select * from function_4(N'2000-01-01 00:00:00.000', N'2010-01-01 00:00:00.000')
-- Write a function that returns a list of students who are currently working with the specified book of a certain author.
CREATE OR ALTER FUNCTION function_5(@AuthorFirstName as nvarchar(50), @AuthorLastName as nvarchar(50), @BookName as nvarchar(50))
RETURNS TABLE
AS
RETURN
(
Select
Library.dbo.Students.FirstName,
Library.dbo.Students.LastName
from
Library.dbo.Students
Inner Join Library.dbo.S_Cards
ON
Library.dbo.Students.Id=Library.dbo.S_Cards.Id_Student
Inner Join Library.dbo.Books
ON
Library.dbo.Books.Id=Library.dbo.S_Cards.Id_Book
Inner Join Library.dbo.Authors
ON
Library.dbo.Authors.Id=Library.dbo.Books.Id_Author
where
Library.dbo.Authors.FirstName=@AuthorFirstName
AND
Library.dbo.Authors.LastName=@AuthorLastName
AND
Library.dbo.Books.[Name]=@BookName
)
select * from function_5(N'Markus', N'Herhager', N'Mathcad 2000')
-- Write a function that returns information about publishers whose total number of pages of books issued by them is greater than N
CREATE OR ALTER FUNCTION function_6(@SumbiggerthanN as bigint)
RETURNS TABLE
AS
RETURN
(
Select
SUM(Pages) as [SUM(Pages)],
Library.dbo.Press.[Name] as Publisher
from
Library.dbo.Books
Inner Join Library.dbo.Press
ON
Library.dbo.Books.Id_Press=Library.dbo.Press.Id
GROUP BY
Library.dbo.Press.Id,
Library.dbo.Press.[Name]
Having
SUM(Pages)>@SumbiggerthanN
)
select * from function_6(100)
-- Write a function that returns information about the most popular author among students and about the number of books of this author taken in the library
CREATE OR ALTER FUNCTION function_7_1()
RETURNS nvarchar (50)
AS
BEGIN
declare @AuthorFulltName as nvarchar(50)
Select
TOP(1)
@AuthorFulltName= Library.dbo.Authors.FirstName + ' ' + Library.dbo.Authors.LastName
from
Library.dbo.Students
Inner Join Library.dbo.S_Cards
ON
Library.dbo.Students.Id=Library.dbo.S_Cards.Id_Student
Inner Join Library.dbo.Books
ON
Library.dbo.Books.Id=Library.dbo.S_Cards.Id_Book
Inner Join Library.dbo.Authors
ON
Library.dbo.Authors.Id=Library.dbo.Books.Id_Author
RETURN @AuthorFulltName
END
declare @result_7 nvarchar(50)
exec @result_7 = function_7_1
print @result_7
CREATE OR ALTER FUNCTION function_7_2()
RETURNS TABLE
AS
RETURN
(
Select
TOP(1)
Library.dbo.Authors.FirstName,
Library.dbo.Authors.LastName,
Library.dbo.Books.[Name],
Library.dbo.Books.Quantity
from
Library.dbo.Students
Inner Join Library.dbo.S_Cards
ON
Library.dbo.Students.Id=Library.dbo.S_Cards.Id_Student
Inner Join Library.dbo.Books
ON
Library.dbo.Books.Id=Library.dbo.S_Cards.Id_Book
Inner Join Library.dbo.Authors
ON
Library.dbo.Authors.Id=Library.dbo.Books.Id_Author
)
select * from function_7_2()
-- Write a function that returns a list of books that were taken by both teachers and students.
CREATE OR ALTER FUNCTION function_8()
RETURNS TABLE
AS
RETURN
(
Select
Top(2)
Library.dbo.Books.Id,
Library.dbo.Books.Name
from
Library.dbo.Teachers
Inner Join Library.dbo.T_Cards
ON
Library.dbo.Teachers.Id=Library.dbo.T_Cards.Id_Teacher
Inner Join Library.dbo.Books
ON
Library.dbo.Books.Id=Library.dbo.T_Cards.Id_Book
INTERSECT
Select Distinct
Library.dbo.Books.Id,
Library.dbo.Books.Name
from
Library.dbo.Students
Inner Join Library.dbo.S_Cards
ON
Library.dbo.Students.Id=Library.dbo.S_Cards.Id_Student
Inner Join Library.dbo.Books
ON
Library.dbo.Books.Id=Library.dbo.S_Cards.Id_Book
)
select * from function_8()
-- Write a function that returns the number of students who did not take books.
CREATE OR ALTER FUNCTION function_9_1()
RETURNS int
AS
BEGIN
declare @count as int
Select
@count =Count(*)
from
Library.dbo.Students
Inner Join Library.dbo.S_Cards
ON
Library.dbo.Students.Id=Library.dbo.S_Cards.Id_Student
Inner Join Library.dbo.Books
ON
Library.dbo.Books.Id=Library.dbo.S_Cards.Id_Book
where Library.dbo.S_Cards.DateIn is null
RETURN @count
END
declare @result_9 int
exec @result_9 = function_9_1
print @result_9
CREATE OR ALTER FUNCTION function_9_2()
RETURNS TABLE
AS
RETURN
(
Select
Count(*) as [Count]
from
Library.dbo.Students
Inner Join Library.dbo.S_Cards
ON
Library.dbo.Students.Id=Library.dbo.S_Cards.Id_Student
Inner Join Library.dbo.Books
ON
Library.dbo.Books.Id=Library.dbo.S_Cards.Id_Book
where Library.dbo.S_Cards.DateIn is null
)
select * from function_9_2()
-- Write a function that returns a list of librarians and the number of books issued by each of them
CREATE OR ALTER FUNCTION function_10()
RETURNS TABLE
AS
RETURN
(
Select
Convert(nvarchar(100), COUNT(*)) + ' book given to Students'AS [book count],
Libs.FirstName
from
Library.dbo.Libs
Inner Join Library.dbo.S_Cards
ON
Library.dbo.Libs.Id=Library.dbo.S_Cards.Id_Lib
Inner Join Library.dbo.Books
ON
Library.dbo.S_Cards.Id_Book=Library.dbo.Books.Id
GROUP BY
Libs.Id,
Libs.FirstName
Union ALL
Select
Convert(nvarchar(100), COUNT(*)) + ' book given to Teachers' AS [book count] ,
Libs.FirstName
from
Library.dbo.Libs
Inner Join Library.dbo.T_Cards
ON
Library.dbo.Libs.Id=Library.dbo.T_Cards.Id_Lib
Inner Join Library.dbo.Books
ON
Library.dbo.T_Cards.Id_Book=Library.dbo.Books.Id
Group by
Libs.Id,
Libs.FirstName
)
select * from function_10()