-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLQuery.sql
53 lines (45 loc) · 1.42 KB
/
SQLQuery.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
CREATE VIEW TeacherAndBookName AS
SELECT t.last_name [Фамилия], b.name [Название книги]
FROM Teacher t JOIN T_Cards t_c
ON t.id = t_c.id_teacher
JOIN Book b
ON t_c.id_book = b.id;
CREATE VIEW StudentsWhoDidNotReturnBooks AS
SELECT s.last_name, s_c.date_in
FROM Student s JOIN S_Cards s_c
ON s.id = s_c.id_student
WHERE s_c.date_in IS NULL;
CREATE VIEW StudentsWhoNeverBorrowedBooks AS
SELECT s.last_name
FROM Student s
WHERE s.id NOT IN (SELECT s_c.id_student FROM S_Cards s_c WHERE s_c.id_student IS NOT NULL)
CREATE VIEW MostActiveLibrarian AS
SELECT TOP 1 last_name, SUM(amount) AS total_count
FROM
(SELECT l.last_name, COUNT(t_c.id_librarian) AS amount
FROM Librarian l JOIN T_Cards t_c
ON l.id = t_c.id_librarian
GROUP BY l.last_name
UNION ALL
SELECT l.last_name, COUNT(s_c.id_librarian)
FROM Librarian l JOIN S_Cards s_c
ON l.id = s_c.id_librarian
GROUP BY l.last_name) AS temp_result
GROUP BY last_name
ORDER BY 2 DESC
CREATE VIEW MostResponsibleLibrarian AS
SELECT TOP 1 last_name, SUM(amount) AS total_count
FROM
(SELECT l.last_name, COUNT(t_c.id_librarian) AS amount
FROM Librarian l JOIN T_Cards t_c
ON l.id = t_c.id_librarian
WHERE t_c.date_in IS NOT NULL
GROUP BY l.last_name
UNION ALL
SELECT l.last_name, COUNT(s_c.id_librarian)
FROM Librarian l JOIN S_Cards s_c
ON l.id = s_c.id_librarian
WHERE s_c.date_in IS NOT NULL
GROUP BY l.last_name) AS temp_result
GROUP BY last_name
ORDER BY 2 DESC