DatabaseCreation: VIEWS.sql

File VIEWS.sql, 4.2 KB (added by 231076, 6 days ago)
Line 
1CREATE VIEW view_admin AS
2SELECT
3 u.user_id,
4 u.username,
5 u.email,
6 m.member_number,
7 COUNT(lh.loan_id) AS vkupno_pozajmuvanja,
8 COUNT(f.fine_id) AS neplateni_kazni,
9 SUM(f.amount) AS vkupen_dolg
10FROM app_user u
11JOIN member m ON m.user_id = u.user_id
12LEFT JOIN loan_history lh ON lh.member_user_id = u.user_id
13LEFT JOIN fines f ON f.loan_id = lh.loan_id
14 AND (f.status = 'unpaid' OR f.status = 'overdue')
15GROUP BY
16 u.user_id,
17 u.username,
18 u.email,
19 m.member_number;
20
21
22CREATE OR REPLACE VIEW view_myreservations AS
23SELECT
24 r.reservation_id,
25 r.member_user_id as member_user_id,
26 b.title,
27 r.reservation_date,
28 r.expiration_date,
29 r.status
30FROM reservation r
31JOIN book b ON b.barcode = r.barcode;
32
33
34CREATE VIEW view_currentloans AS
35SELECT
36 lh.member_user_id AS korisnik_id,
37 b.title,
38 lh.borrow_date,
39 lh.due_date,
40 lh.due_date::date - CURRENT_DATE AS ostanati_denovi
41FROM loan_history lh
42JOIN book_copy bc ON bc.copy_id = lh.copy_id
43JOIN book b ON b.barcode = bc.barcode
44WHERE lh.return_date IS NULL
45 AND lh.status = 'borrowed'
46 AND lh.due_date::date >= CURRENT_DATE;
47
48CREATE VIEW view_bookcatalog AS
49SELECT DISTINCT
50 b.barcode,
51 b.title AS book_title,
52 a.first_name || ' ' || a.last_name AS avtor,
53 c.name AS kategorija,
54 g.name AS zhanr,
55 p.name AS izdavach,
56 b.publication_year
57FROM book b
58LEFT JOIN book_author ba ON ba.barcode = b.barcode
59LEFT JOIN author a ON a.author_id = ba.author_id
60LEFT JOIN category_book cb ON cb.barcode = b.barcode
61LEFT JOIN category c ON c.category_id = cb.category_id
62LEFT JOIN book_genre bg ON bg.barcode = b.barcode
63LEFT JOIN genre g ON g.genre_id = bg.genre_id
64LEFT JOIN publisher p ON p.publisher_id = b.publisher_id;
65
66
67CREATE VIEW View_MyNotifications AS
68SELECT
69 n.notification_id,
70 n.member_user_id,
71 n.notification_type,
72 n.status,
73 n.created_at
74FROM notification n;
75
76
77CREATE OR REPLACE VIEW view_userborrowhistory AS
78SELECT
79 lh.loan_id,
80 lh.member_user_id,
81
82 bc.barcode,
83 b.title,
84
85 lh.borrow_date,
86 lh.due_date,
87 lh.return_date,
88
89 lh.status AS loan_status,
90
91 CASE
92 WHEN lh.return_date IS NOT NULL
93 THEN lh.return_date - lh.borrow_date
94 ELSE CURRENT_DATE - lh.borrow_date
95 END AS denovi_traenje,
96
97 CASE
98 WHEN lh.return_date IS NOT NULL THEN
99 CASE
100 WHEN lh.return_date > lh.due_date
101 THEN lh.return_date - lh.due_date
102 ELSE 0
103 END
104 ELSE
105 CASE
106 WHEN CURRENT_DATE > lh.due_date
107 THEN CURRENT_DATE - lh.due_date
108 ELSE 0
109 END
110 END AS denovi_kasni
111
112FROM loan_history lh
113JOIN book_copy bc ON lh.copy_id = bc.copy_id
114JOIN book b ON bc.barcode = b.barcode;
115
116
117CREATE OR REPLACE VIEW view_bookmonthly AS
118WITH counts AS (
119 SELECT
120 EXTRACT(YEAR FROM bvl.view_timestamp) AS year,
121 EXTRACT(MONTH FROM bvl.view_timestamp) AS month,
122 bvl.barcode,
123 b.title,
124 COUNT(*) AS view_count
125 FROM book_view_log bvl
126 JOIN book b ON b.barcode = bvl.barcode
127 GROUP BY year, month, bvl.barcode, b.title
128),
129ranked AS (
130 SELECT
131 *,
132 ROW_NUMBER() OVER (
133 PARTITION BY year, month
134 ORDER BY view_count DESC
135 ) AS max
136 FROM counts
137)
138SELECT
139 year,
140 month,
141 barcode,
142 title,
143 view_count
144FROM ranked
145WHERE max = 1
146ORDER BY year, month;
147
148
149
150CREATE VIEW view_upcomingevents AS
151SELECT
152 event_id,
153 title,
154 event_date,
155 start_time,
156 end_time,
157 location,
158 max_seats,
159 available_seats,
160 CASE
161 WHEN available_seats > 0 THEN 'AVAILABLE'
162 ELSE 'FULL'
163 END AS availability_status
164FROM event
165WHERE event_date >= CURRENT_DATE;
166
167
168CREATE VIEW view_myfinesandfees AS
169SELECT
170 lh.member_user_id AS korisnik_id,
171 b.title AS naslov,
172 f.amount,
173 f.status AS status_kazna,
174 f.payment_due_date
175FROM fines f
176JOIN loan_history lh ON lh.loan_id = f.loan_id
177JOIN book_copy bc ON bc.copy_id = lh.copy_id
178JOIN book b ON b.barcode = bc.barcode
179WHERE f.status IN ('unpaid', 'overdue');
180
181