-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL View.sql
More file actions
145 lines (120 loc) · 3.91 KB
/
SQL View.sql
File metadata and controls
145 lines (120 loc) · 3.91 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
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
create database Hospital;
use Hospital;
create table Doctor(
Doctor_id int,
Name varchar(255),
Specialisation varchar(255),
Phone varchar(255),
PRIMARY KEY(Doctor_id)
);
create table Patient(
Patient_id int,
Name varchar(255),
Birth_date Date,
Gender varchar(255),
Address varchar(255),
PRIMARY KEY(Patient_id)
);
create table Appoinmnet(
Appoinmnet_id int,
Patient_id int,
Doctor_id int,
Appoinmnet_date date,
Status varchar(100),
PRIMARY KEY(Appoinmnet_id)
);
create table Treatment(
Treatment_id int,
Appointment_id int,
Description varchar(255),
Cost FLOAT(10,2),
PRIMARY KEY(Treatment_id)
);
create table Prescription(
Prescription_id int,
Appoinmnet_id int,
Medicine varchar(255),
Desage varchar(255),
PRIMARY KEY(Prescription_id)
);
ALTER TABLE Appoinmnet
ADD FOREIGN KEY (Patient_id)
REFERENCES Patient(Patient_id);
ALTER TABLE Appoinmnet
ADD FOREIGN KEY (Doctor_id)
REFERENCES Doctor(Doctor_id);
ALTER TABLE Prescription
ADD FOREIGN KEY (Appoinmnet_id)
REFERENCES Appoinmnet(Appoinmnet_id);
ALTER TABLE Treatment
ADD FOREIGN KEY (Appointment_id)
REFERENCES Appoinmnet(Appoinmnet_id);
CREATE VIEW View_Doctor AS
SELECT *
FROM doctor;
select * from View_Doctor;
CREATE VIEW View_FemalePateints AS
SELECT *
FROM Patient
WHERE Gender = 'Female';
SELECT * from View_FemalePateints;
-- View 3: All appointments for a given patient (replace 3 with parameter when querying)
CREATE VIEW View_AppointmentsByPatient AS
SELECT * FROM Appoinmnet;
SELECT * FROM View_AppointmentsByPatient;
-- When you query this view, use:
-- SELECT * FROM View_AppointmentsByPatient WHERE Patient_id = 3;
-- View 4: Treatments with costs
CREATE VIEW View_TreatmentsCost AS
SELECT Treatment_id, Description, Cost FROM Treatment;
SELECT * FROM View_TreatmentsCost;
-- View 5: Total cost of treatments per patient
CREATE VIEW View_TotalTreatmentCostPerPatient AS
SELECT A.Patient_id, SUM(T.Cost) AS TotalCost
FROM Treatment T
JOIN Appoinmnet A ON T.Appointment_id = A.Appoinmnet_id
GROUP BY A.Patient_id;
SELECT * FROM ew_TotalTreatmentCostPerPatient;
-- View 6: Doctors specialized in Cardiology
CREATE VIEW View_CardiologyDoctors AS
SELECT * FROM Doctor WHERE Specialisation = 'Cardiology';
SELECT * FROM
-- View 7: Appointments after a certain date
CREATE VIEW View_RecentAppointments AS
SELECT * FROM Appoinmnet
WHERE Appoinmnet_date > '2025-04-15';
SELECT * FROM View_RecentAppointments;
-- View 9: Patients with appointments with Neurology doctors and status scheduled
CREATE VIEW View_PatientsWithNeurologyAppointments AS
SELECT DISTINCT P.*
FROM Patient P
JOIN Appoinmnet A ON P.Patient_id = A.Patient_id
JOIN Doctor D ON A.Doctor_id = D.Doctor_id
WHERE D.Specialisation = 'Neurology' AND A.Status = 'Scheduled';
SELECT * FROM View_PatientsWithNeurologyAppointments;
-- View 11: Doctors and their total appointments
CREATE VIEW View_DoctorsAppointmentsCount AS
SELECT D.Doctor_id, D.Name, COUNT(A.Appoinmnet_id) AS TotalAppointments
FROM Doctor D
LEFT JOIN Appoinmnet A ON D.Doctor_id = A.Doctor_id
GROUP BY D.Doctor_id, D.Name;
SELECT * FROM
-- View 12: Most commonly prescribed medicine with count
CREATE VIEW View_MedicineFrequency AS
SELECT Medicine, COUNT(*) AS Frequency
FROM Prescription
GROUP BY Medicine;
SELECT * FROM View_MedicineFrequency
-- View 15: Average cost of treatments per appointment
CREATE VIEW View_AvgTreatmentCostPerAppointment AS
SELECT Appointment_id, AVG(Cost) AS AvgCost
FROM Treatment
GROUP BY Appointment_id;
SELECT * FROM View_AvgTreatmentCostPerAppointment;
-- View 17: Treatments with patient info
CREATE VIEW View_TreatmentsWithPatients AS
SELECT T.Treatment_id, T.Description, P.Patient_id, P.Name AS PatientName
FROM Treatment T
JOIN Appoinmnet A ON T.Appointment_id = A.Appoinmnet_id
JOIN Patient P ON A.Patient_id = P.Patient_id;
SELECT * FROM View_TreatmentsWithPatients;