-
Notifications
You must be signed in to change notification settings - Fork 2
/
storedProcedure.sql
90 lines (65 loc) · 1.96 KB
/
storedProcedure.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
-- by RESOURCE
DELIMITER //
CREATE PROCEDURE ResourceTotal(IN lab_id INT(10))
BEGIN
DECLARE curMonth INT;
DECLARE curYear INT;
SET curMonth = (select MONTH(NOW()));
SET curYear = (select YEAR(NOW()));
select resources.name,count(resources.id) as total
from resources,bookings
where bookings.resource_id = resources.id
AND resources.laboratory_id = lab_id
AND MONTH(bookings.start_date) = curMonth
AND YEAR(bookings.start_date) = curYear
group by resources.name;
END //
DELIMITER ;
-- by USER
DELIMITER //
CREATE PROCEDURE UserTotal(IN lab_id INT(10))
BEGIN
DECLARE curMonth INT;
DECLARE curYear INT;
SET curMonth = (select MONTH(NOW()));
SET curYear = (select YEAR(NOW()));
select bookings.user_id,count(resources.id) as total
from resources,bookings
where bookings.resource_id = resources.id
AND resources.laboratory_id = lab_id
AND MONTH(bookings.start_date) = curMonth
AND YEAR(bookings.start_date) = curYear
group by bookings.user_id;
END //
DELIMITER ;
-- by MONTH
DELIMITER //
CREATE PROCEDURE MonthTotal(IN lab_id INT(10))
BEGIN
DECLARE curYear INT;
SET curYear = (select YEAR(NOW()));
select MONTH(bookings.start_date) as month ,count(bookings.id) as total
from resources,bookings
where bookings.resource_id = resources.id
AND resources.laboratory_id = lab_id
AND YEAR(bookings.start_date) = curYear
group by MONTH(bookings.start_date);
END //
DELIMITER ;
-- by WEEK
DELIMITER //
CREATE PROCEDURE WeekTotal(IN lab_id INT(10))
BEGIN
DECLARE curMonth INT;
DECLARE curYear INT;
SET curMonth = (select MONTH(NOW()));
SET curYear = (select YEAR(NOW()));
select DAYOFWEEK(bookings.start_date) as day ,count(bookings.id) as total
from resources,bookings
where bookings.resource_id = resources.id
AND resources.laboratory_id = lab_id
AND MONTH(bookings.start_date) = curMonth
AND YEAR(bookings.start_date) = curYear
group by DAYOFWEEK(bookings.start_date);
END //
DELIMITER ;