-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViews.sql
More file actions
72 lines (64 loc) · 1.77 KB
/
Copy pathViews.sql
File metadata and controls
72 lines (64 loc) · 1.77 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
-- CREATING VIEW TABLES FOR FURTHER USE CASES.
-- A PATEINT APPOINTMENT VIEW
-- 1. DOCTORS CAN VIEW THIER SCHEDULE AND
-- 2. GET THE DETAILS ABOUT PATEIENT AND INTENTION OF APPOINTMENT.
CREATE VIEW PatientAppointmentDetails AS
SELECT
p.PatientID,
p.FirstName + ' ' + p.LastName AS PatientName,
a.AppointmentDate,
a.AppointmentTime,
d.FirstName + ' ' + d.LastName AS DoctorName,
d.Specialization,
a.Purpose
FROM
Patients p
INNER JOIN Appointments a ON p.PatientID = a.PatientID
INNER JOIN Doctors d ON a.DoctorID = d.DoctorID
WHERE
a.Status = 'Scheduled'
AND a.AppointmentDate >= GETDATE();
GO
-- COMBINES INVENTORY WITH SELF JOIN
-- 1. FOR EXPIRED MEDICATION.
-- 2. FOR LOW STOCK.
CREATE VIEW MedicationInventoryStatus AS
SELECT
i.ItemID,
i.ItemName,
i.Quantity,
i.ExpiryDate,
s.FirstName + ' ' + s.LastName AS ResponsibleStaff
FROM
Inventory i
LEFT JOIN Staff s ON s.Department = 'Pharmacy'
WHERE
i.Category = 'Medication'
AND i.Quantity < 20
AND i.ExpiryDate > GETDATE();
GO
-- JOINING MEDICAL RECORDS, BILLINGS AND PATIENT.
-- 1. TRACK OUTSTANDING PAYMENTS.
-- 2. IMPROVE FINANCIAL OVERSIGHT.
CREATE VIEW UnpaidMedicalBillingOverview AS
SELECT
mr.RecordID,
mr.PatientID,
CONCAT(p.FirstName, ' ', p.LastName) AS PatientName,
mr.Diagnosis,
mr.Prescription,
b.BillID,
b.TotalAmount,
b.PaymentStatus
FROM
MedicalRecords mr
INNER JOIN
Patients p ON mr.PatientID = p.PatientID
INNER JOIN
Billing b ON mr.PatientID = b.PatientID AND mr.RecordID = b.AppointmentID
WHERE
b.PaymentStatus = 'Pending' OR b.PaymentStatus = 'Unpaid';
GO
SELECT * FROM MedicationInventoryStatus;
SELECT * FROM PatientAppointmentDetails;
SELECT * FROM UnpaidMedicalBillingOverview;