-
Notifications
You must be signed in to change notification settings - Fork 0
/
Query.Library.sql
97 lines (75 loc) · 2.89 KB
/
Query.Library.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
-- =FINDING BOOK + BRCH LOC
CREATE PROC uspGETBookAndBranch
@Title VARCHAR(30),
@BranchName VARCHAR(30)
--VARCHAR CONSIST CHK = OK
AS
SELECT
a1.books_Title, a2.branch_Name, c.number_Copies --2.) =HOW MANY COPIES
FROM tbl_books a1
JOIN tbl_bookcopies c ON c.books_ID = a1. books_ID
INNER JOIN tbl_library a2 ON a2.branch_ID = c.branch_ID
WHERE books_Title = @Title and branch_Name = @BranchName
GO
EXEC uspGETBookAndBranch 'The Lost Tribe', 'Sharpstown' -- 1.) 2.) HOW MANY COPIES "THE LOST TRIBE" BKS_TITLE @ BRANCHNAME "SHARPSTOWN"
--3.) RETRIEVE NAMES OF BORROWERS W/O ANY BKS CHECKED OUT
CREATE PROC uspNoBooks
AS
SELECT
a1.borrower_Name, a2.card_Num, a2.date_Out, a2. date_Due
FROM tbl_borrower a1
LEFT JOIN tbl_bookloans a2 ON a1.card_Num = a2. card_Num
WHERE a1.card_Num NOT IN (SELECT card_Num FROM tbl_bookloans) -- = W/OUT ANY BKS
EXEC uspNoBooks
--RETRIEVE EA BK DUE TODAY @ VARIOUS BRCH LOC'S
CREATE PROC uspBooksDueToday --4.) CREATING DUETODAY PROC
@BranchName VARCHAR(30),
@DateDue VARCHAR(10)
--VARCHAR CONSIST CHK = OK
AS
SELECT --4.) RETRIEVE BK TITLE + BORROWER NAME + BORROWER ADDY FOR EA BOOK FROM "Sharpstown" BRCH W/"DATEDUE" = TODAY
a1.branch_Name, a2.date_Due, c.books_Title, d.borrower_Name, d.borrower_Addy
FROM tbl_library a1
JOIN tbl_bookloans a2 ON a2.branch_ID = a1. branch_ID
JOIN tbl_books c ON c.books_ID = a2.books_ID
JOIN tbl_borrower d ON a2.card_Num = d.card_Num
WHERE branch_Name = @BranchName and date_Due = @DateDue
EXEC uspBooksDueToday 'Sharpstown', '10/31/2018'
--5.) RETRIEVE BRANCH NAME + TOTAL BKS CHECKED OUT FROM BRCH "Sharpstown"
CREATE PROCEDURE uspTotalBookLoans
@BranchName VARCHAR(30)
--VARCHAR CONSIST CHK = OK
AS
SELECT
a1.branch_Name, COUNT(books_Title)
FROM tbl_library a1
JOIN tbl_bookloans a2 ON a2.branch_ID = a1. branch_ID
JOIN tbl_books c ON c.books_ID = a2.books_ID
WHERE branch_name = @BranchName
GROUP BY a1.branch_Name
EXEC uspTotalBookLoans 'Sharpstown'
--EXEC "" '@BranchName'
--6.) RETRIEVE NAMES + ADDYS + # BKS CHKD OUT FOR ALL BRRWERS WHO HAVE > 5 BKS CHKD OUT
CREATE PROCEDURE uspGREATERThanFiveBooks
AS
SELECT borrower_Name, c.card_Num, c.borrower_Addy FROM
tbl_books a
JOIN tbl_bookloans b ON a.books_ID = b.books_ID
JOIN tbl_borrower c ON c.card_Num = b.card_Num
GROUP BY borrower_Name, c.card_Num, c.borrower_Addy
HAVING COUNT(b.card_Num) > 5 -- = BORROWER ID W/ >5 BOOKS CHKD OUT
ORDER BY c.card_Num ASC
EXEC uspGREATERThanFiveBooks
--RETRIEVE # OF COPIES OWNED BY BRCH
CREATE PROCEDURE uspNumCopiesAtBranch
@Title VARCHAR(30),
@BranchName VARCHAR(30)
-- VARCHAR CONSIST CHK = OK
AS
SELECT authors_Name, books_Title, num_Copies, branch_Name
FROM tbl_books a
JOIN tbl_authors e ON e.books_ID = a.books_ID
JOIN tbl_bookcopies b ON a.books_ID = b.books_ID
JOIN tbl_library d ON d.branch_ID = b.branch_ID
WHERE books_Title = @Title and branch_Name = @BranchName
EXEC uspNumCopiesAtBranch 'IT', 'Central'