-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreports.sql
196 lines (172 loc) · 5.9 KB
/
reports.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use BloodTransfusion;
GO
-- Get the list of donors
DROP PROCEDURE IF EXISTS GetListOfDonors;
GO
CREATE PROCEDURE GetListOfDonors
AS
BEGIN
SET NOCOUNT ON;
SELECT * FROM BloodTransporter transporters
WHERE (
(SELECT COUNT(*) FROM Donation WHERE donorId = transporters.nationalId) > 0
);
RETURN
END
Go
EXEC GetListOfDonors;
GO
DROP PROCEDURE IF EXISTS GetTimeOfNextDonation
GO
-- Get the time for next donation
CREATE PROCEDURE GetTimeOfNextDonation
@blood_transporter_id NVARCHAR(10),
@blood_product_name NVARCHAR(64),
@date_value DATE OUTPUT
AS
BEGIN
SET NOCOUNT ON;
Declare @last_donation_time DATE
Declare @waiting_time INT
SELECT @last_donation_time = MAX(donationTime) FROM Donation donation WHERE donation.donorId = @blood_transporter_id;
SELECT @waiting_time = waitingTime FROM BloodProduct WHERE productName = @blood_product_name;
SELECT @date_value = DATEADD(day, @waiting_time, @last_donation_time)
END
Go
declare @date DATE;
exec GetTimeOfNextDonation @blood_transporter_id = N'0021190941', @blood_product_name = N'پلاسما', @date_value = @date OUTPUT;
SELECT @date as nextDonationTime
GO
DROP FUNCTION IF EXISTS GetBloodPacketsAccordingTypeAndProductAndCity
GO
-- Get closest to expiration blood packet for next donation
CREATE FUNCTION GetBloodPacketsAccordingTypeAndProductAndCity (
@blood_type NVARCHAR(64),
@blood_product NVARCHAR(64),
@city_name_of_requester NVARCHAR(64)
) RETURNS TABLE
AS
RETURN
(
SELECT blood_packet.*
FROM BloodPacket blood_packet,
Donation donation,
BloodTransporter blood_transporter,
BloodBank blood_bank,
City city
WHERE
(
blood_packet.donationId = donation.id AND
donation.donorId = blood_transporter.nationalId AND
blood_transporter.bloodType = @blood_type AND
donation.bloodProduct = @blood_product AND
blood_packet.locatedAt = blood_bank.cityId AND
blood_bank.cityId = city.id AND
city.cityName = @city_name_of_requester
)
)
Go
SELECT * FROM GetBloodPacketsAccordingTypeAndProductAndCity(N'A+', N'پلاسما', N'شهر۱');
GO
DROP PROCEDURE IF EXISTS OrderNecessaryBloodProductsInCity;
GO
-- Get most necessary blood type according to city
CREATE PROCEDURE OrderNecessaryBloodProductsInCity
@blood_type NVARCHAR(3),
@city_name_of_requester NVARCHAR(64)
AS
BEGIN
SET NOCOUNT ON;
SELECT blood_product.*, (
(
SELECT COALESCE(SUM(need.needPriority), 0)
FROM Need need,
Hospital hospital,
City city
WHERE
(
need.neededBy = hospital.id AND
hospital.cityId = city.id AND
city.cityName = @city_name_of_requester AND
need.bloodProduct = blood_product.productName AND
need.bloodType = @blood_type
)
)
-
(
SELECT COUNT(*) FROM PresentBloodPacket blood_packet,
Donation donation,
BloodTransporter blood_transporter,
BloodBank blood_bank,
City city
WHERE
(
blood_packet.donationId = donation.id AND
donation.bloodProduct = blood_product.productName AND
donation.donorId = blood_transporter.nationalId AND
blood_transporter.bloodType = @blood_type AND
blood_packet.locatedAt = blood_bank.id AND
blood_bank.cityId = city.id AND
city.cityName = @city_name_of_requester
)
)
) AS bloodProductNeed FROM BloodProduct blood_product ORDER BY bloodProductNeed DESC
END
Go
exec OrderNecessaryBloodProductsInCity @city_name_of_requester = N'شهر۱', @blood_type = N'A+';
DROP PROCEDURE IF EXISTS OrderNecessaryBloodProducts;
GO
-- Get most necessary blood types in all cities
CREATE PROCEDURE OrderNecessaryBloodProducts
@blood_type NVARCHAR(3)
AS
BEGIN
SET NOCOUNT ON;
SELECT blood_product.*, (
(
SELECT COALESCE(SUM(need.needPriority), 0) FROM Need need
WHERE
(
need.bloodProduct = blood_product.productName AND
need.bloodType = @blood_type
)
)
-
(
SELECT COUNT(*) FROM PresentBloodPacket blood_packet,
Donation donation,
BloodTransporter blood_transporter
WHERE
(
blood_packet.donationId = donation.id AND
donation.bloodProduct = blood_product.productName AND
donation.donorId = blood_transporter.nationalId AND
blood_transporter.bloodType = @blood_type
)
)
) AS bloodProductNeed FROM BloodProduct blood_product ORDER BY bloodProductNeed DESC
END
Go
exec OrderNecessaryBloodProducts @blood_type = N'A+'
GO
DROP PROCEDURE IF EXISTS GiveNursesLevels;
GO
CREATE PROCEDURE GiveNursesLevels
@root_nurse_id INTEGER
AS
BEGIN
WITH CTE_NumOfBloodPacketSignedByInferiors AS
(
SELECT employeeId, 1 AS nurseLevel FROM Nurse WHERE employeeId = @root_nurse_id
UNION ALL
SELECT
nurse.employeeId, CTE_NumOfBloodPacketSignedByInferiors.nurseLevel + 1
FROM Nurse Nurse
INNER JOIN CTE_NumOfBloodPacketSignedByInferiors
ON nurse.supervisedBy = CTE_NumOfBloodPacketSignedByInferiors.employeeId
)
SELECT * FROM CTE_NumOfBloodPacketSignedByInferiors
END
GO
EXEC GiveNursesLevels @root_nurse_id = 1
GO