-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary Functions.sql
More file actions
114 lines (105 loc) · 3.15 KB
/
Library Functions.sql
File metadata and controls
114 lines (105 loc) · 3.15 KB
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
create function Library.showAllBooksEdition()
returns table
as return(
select ba.editionID, b.title, b.author, ba.editionNumber, ba.availableCopies, sl.aisle, sl.shelfNumber
from Library.BooksAvailable ba
join Library.Books b on ba.bookID = b.bookID
left join Library.ShelfLocation sl on ba.editionID = sl.editionID
);
GO
create function Library.getAccID(@stuID INT)
returns INT
as begin
declare @AccountID INT;
select @AccountID = Library.Account.accountID
from Library.Account
where Library.Account.personID = @stuID;
return @AccountID;
end;
GO
create function Library.getTotalFineByStuID(@studentID INT)
returns INT
as begin
declare @AccountID INT;
set @AccountID = Library.getAccID(@studentID);
return Library.getTotalFineByAccID(@AccountID);
end;
GO
create function Library.getTotalFineByAccID(@AccountID INT)
returns INT
as begin
return(
select ISNULL(sum(amount), 0)
from Library.Fines where accountID = @AccountID and status = 'notPaid'
);
end;
GO
create function Library.showFinesByAccID(@accountID INT)
returns table
as return(
select fineID, amount
from Library.Fines where Library.Fines.accountID = @accountID and status = 'notPaid'
);
GO
create function Library.getBorrowHistory(@accountID INT)
returns table
as return(
select
b.title, ba.editionNumber, br.borrowDate, br.dueDate, br.returnDate, br.status, sl.aisle, sl.shelfNumber
from Library.BorrowRecords br
join Library.BooksAvailable ba on br.editionID = ba.editionID
join Library.Books b on ba.bookID = b.bookID
left join Library.ShelfLocation sl on br.editionID = sl.editionID
where br.accountID = @accountID
);
GO
create function Library.GetRecommendedBooks(@studentID INT)
returns table
as return(
with UserBooks as(
select editionID
from Library.BorrowRecords
where accountID = Library.getAccID(@studentID)
),
SimilarUsers as(
select br.accountID
from Library.BorrowRecords br
join UserBooks ub on br.editionID = ub.editionID
where br.accountID <> Library.getAccID(@studentID)
group by br.accountID
having count(DISTINCT br.editionID) >= 2
),
Recommended as(
select br.editionID, count(*) AS freq
from Library.BorrowRecords br
where br.accountID in(select accountID from SimilarUsers)
and br.editionID not in(select editionID from UserBooks)
group by br.editionID
)
select top 3
r.editionID, b.title, b.author, sl.aisle, sl.shelfNumber, r.freq
from Recommended r
join Library.BooksAvailable ba on ba.editionID = r.editionID
join Library.Books b on b.bookID = ba.bookID
left join Library.ShelfLocation sl on r.editionID = sl.editionID
ORDER BY r.freq desc
);
GO
create function Library.getAllAccountsTotalFine()
returns table
as return(
select accountID, sum(amount) as sum
from Library.Fines where status = 'notPaid'
group by accountID
);
GO
create function Library.GetBookLocation(@editionID INT)
returns table
as return(
select
l.aisle,
l.shelfNumber
from Library.ShelfLocation l
where l.editionID = @editionID
);
GO