import time
import os
time_start_notebook = time.time()
import numpy as np
import pandas as pd
# Jupyter notebook settings for pandas
pd.set_option('display.max_columns', 200)
pd.set_option('display.max_rows', 100)
pd.set_option('display.max_colwidth', 100)
from datetime import datetime
from dateutil.relativedelta import relativedelta
# last_year = datetime.now() - relativedelta(years=1)
import sqlite3
sqlite3.sqlite_version # sqlite database version 3.29
sqlite3.version # python module version
'2.6.0'
# create db if does not exist
dbname = 'wbhospital.db'
if os.path.isfile(dbname):
os.remove(dbname)
con = sqlite3.connect(dbname)
cur = con.cursor()
data = """
CREATE TABLE Physician (
EmployeeID INTEGER PRIMARY KEY NOT NULL,
Name TEXT NOT NULL,
Position TEXT NOT NULL,
SSN INTEGER NOT NULL
);
CREATE TABLE Department (
DepartmentID INTEGER PRIMARY KEY NOT NULL,
Name TEXT NOT NULL,
Head INTEGER NOT NULL
CONSTRAINT fk_Physician_EmployeeID REFERENCES Physician(EmployeeID)
);
CREATE TABLE Affiliated_With (
Physician INTEGER NOT NULL
CONSTRAINT fk_Physician_EmployeeID REFERENCES Physician(EmployeeID),
Department INTEGER NOT NULL
CONSTRAINT fk_Department_DepartmentID REFERENCES Department(DepartmentID),
PrimaryAffiliation BOOLEAN NOT NULL,
PRIMARY KEY(Physician, Department)
);
CREATE TABLE Procedure (
Code INTEGER PRIMARY KEY NOT NULL,
Name TEXT NOT NULL,
Cost REAL NOT NULL
);
CREATE TABLE Trained_In (
Physician INTEGER NOT NULL
CONSTRAINT fk_Physician_EmployeeID REFERENCES Physician(EmployeeID),
Treatment INTEGER NOT NULL
CONSTRAINT fk_Procedure_Code REFERENCES Procedure(Code),
CertificationDate DATETIME NOT NULL,
CertificationExpires DATETIME NOT NULL,
PRIMARY KEY(Physician, Treatment)
);
CREATE TABLE Patient (
SSN INTEGER PRIMARY KEY NOT NULL,
Name TEXT NOT NULL,
Address TEXT NOT NULL,
Phone TEXT NOT NULL,
InsuranceID INTEGER NOT NULL,
PCP INTEGER NOT NULL
CONSTRAINT fk_Physician_EmployeeID REFERENCES Physician(EmployeeID)
);
CREATE TABLE Nurse (
EmployeeID INTEGER PRIMARY KEY NOT NULL,
Name TEXT NOT NULL,
Position TEXT NOT NULL,
Registered BOOLEAN NOT NULL,
SSN INTEGER NOT NULL
);
CREATE TABLE Appointment (
AppointmentID INTEGER PRIMARY KEY NOT NULL,
Patient INTEGER NOT NULL
CONSTRAINT fk_Patient_SSN REFERENCES Patient(SSN),
PrepNurse INTEGER
CONSTRAINT fk_Nurse_EmployeeID REFERENCES Nurse(EmployeeID),
Physician INTEGER NOT NULL
CONSTRAINT fk_Physician_EmployeeID REFERENCES Physician(EmployeeID),
Start DATETIME NOT NULL,
End DATETIME NOT NULL,
ExaminationRoom TEXT NOT NULL
);
CREATE TABLE Medication (
Code INTEGER PRIMARY KEY NOT NULL,
Name TEXT NOT NULL,
Brand TEXT NOT NULL,
Description TEXT NOT NULL
);
CREATE TABLE Prescribes (
Physician INTEGER NOT NULL
CONSTRAINT fk_Physician_EmployeeID REFERENCES Physician(EmployeeID),
Patient INTEGER NOT NULL
CONSTRAINT fk_Patient_SSN REFERENCES Patient(SSN),
Medication INTEGER NOT NULL
CONSTRAINT fk_Medication_Code REFERENCES Medication(Code),
Date DATETIME NOT NULL,
Appointment INTEGER
CONSTRAINT fk_Appointment_AppointmentID REFERENCES Appointment(AppointmentID),
Dose TEXT NOT NULL,
PRIMARY KEY(Physician, Patient, Medication, Date)
);
CREATE TABLE Block (
Floor INTEGER NOT NULL,
Code INTEGER NOT NULL,
PRIMARY KEY(Floor, Code)
);
CREATE TABLE Room (
Number INTEGER PRIMARY KEY NOT NULL,
Type TEXT NOT NULL,
BlockFloor INTEGER NOT NULL
CONSTRAINT fk_Block_Floor REFERENCES Block(Floor),
BlockCode INTEGER NOT NULL
CONSTRAINT fk_Block_Code REFERENCES Block(Code),
Unavailable BOOLEAN NOT NULL
);
CREATE TABLE On_Call (
Nurse INTEGER NOT NULL
CONSTRAINT fk_Nurse_EmployeeID REFERENCES Nurse(EmployeeID),
BlockFloor INTEGER NOT NULL
CONSTRAINT fk_Block_Floor REFERENCES Block(Floor),
BlockCode INTEGER NOT NULL
CONSTRAINT fk_Block_Code REFERENCES Block(Code),
Start DATETIME NOT NULL,
End DATETIME NOT NULL,
PRIMARY KEY(Nurse, BlockFloor, BlockCode, Start, End)
);
CREATE TABLE Stay (
StayID INTEGER PRIMARY KEY NOT NULL,
Patient INTEGER NOT NULL
CONSTRAINT fk_Patient_SSN REFERENCES Patient(SSN),
Room INTEGER NOT NULL
CONSTRAINT fk_Room_Number REFERENCES Room(Number),
Start DATETIME NOT NULL,
End DATETIME NOT NULL
);
CREATE TABLE Undergoes (
Patient INTEGER NOT NULL
CONSTRAINT fk_Patient_SSN REFERENCES Patient(SSN),
Procedure INTEGER NOT NULL
CONSTRAINT fk_Procedure_Code REFERENCES Procedure(Code),
Stay INTEGER NOT NULL
CONSTRAINT fk_Stay_StayID REFERENCES Stay(StayID),
Date DATETIME NOT NULL,
Physician INTEGER NOT NULL
CONSTRAINT fk_Physician_EmployeeID REFERENCES Physician(EmployeeID),
AssistingNurse INTEGER
CONSTRAINT fk_Nurse_EmployeeID REFERENCES Nurse(EmployeeID),
PRIMARY KEY(Patient, Procedure, Stay, Date)
);
""".split(';')
for i,q in enumerate(data):
if q:
#print(q);break
cur.execute(q)
con.commit()
data = """
INSERT INTO Physician VALUES(1,'John Dorian','Staff Internist',111111111);
INSERT INTO Physician VALUES(2,'Elliot Reid','Attending Physician',222222222);
INSERT INTO Physician VALUES(3,'Christopher Turk','Surgical Attending Physician',333333333);
INSERT INTO Physician VALUES(4,'Percival Cox','Senior Attending Physician',444444444);
INSERT INTO Physician VALUES(5,'Bob Kelso','Head Chief of Medicine',555555555);
INSERT INTO Physician VALUES(6,'Todd Quinlan','Surgical Attending Physician',666666666);
INSERT INTO Physician VALUES(7,'John Wen','Surgical Attending Physician',777777777);
INSERT INTO Physician VALUES(8,'Keith Dudemeister','MD Resident',888888888);
INSERT INTO Physician VALUES(9,'Molly Clock','Attending Psychiatrist',999999999);
INSERT INTO Department VALUES(1,'General Medicine',4);
INSERT INTO Department VALUES(2,'Surgery',7);
INSERT INTO Department VALUES(3,'Psychiatry',9);
INSERT INTO Affiliated_With VALUES(1,1,1);
INSERT INTO Affiliated_With VALUES(2,1,1);
INSERT INTO Affiliated_With VALUES(3,1,0);
INSERT INTO Affiliated_With VALUES(3,2,1);
INSERT INTO Affiliated_With VALUES(4,1,1);
INSERT INTO Affiliated_With VALUES(5,1,1);
INSERT INTO Affiliated_With VALUES(6,2,1);
INSERT INTO Affiliated_With VALUES(7,1,0);
INSERT INTO Affiliated_With VALUES(7,2,1);
INSERT INTO Affiliated_With VALUES(8,1,1);
INSERT INTO Affiliated_With VALUES(9,3,1);
INSERT INTO Procedure VALUES(1,'Reverse Rhinopodoplasty',1500.0);
INSERT INTO Procedure VALUES(2,'Obtuse Pyloric Recombobulation',3750.0);
INSERT INTO Procedure VALUES(3,'Folded Demiophtalmectomy',4500.0);
INSERT INTO Procedure VALUES(4,'Complete Walletectomy',10000.0);
INSERT INTO Procedure VALUES(5,'Obfuscated Dermogastrotomy',4899.0);
INSERT INTO Procedure VALUES(6,'Reversible Pancreomyoplasty',5600.0);
INSERT INTO Procedure VALUES(7,'Follicular Demiectomy',25.0);
INSERT INTO Patient VALUES(100000001,'John Smith','42 Foobar Lane','555-0256',68476213,1);
INSERT INTO Patient VALUES(100000002,'Grace Ritchie','37 Snafu Drive','555-0512',36546321,2);
INSERT INTO Patient VALUES(100000003,'Random J. Patient','101 Omgbbq Street','555-1204',65465421,2);
INSERT INTO Patient VALUES(100000004,'Dennis Doe','1100 Foobaz Avenue','555-2048',68421879,3);
INSERT INTO Nurse VALUES(101,'Carla Espinosa','Head Nurse',1,111111110);
INSERT INTO Nurse VALUES(102,'Laverne Roberts','Nurse',1,222222220);
INSERT INTO Nurse VALUES(103,'Paul Flowers','Nurse',0,333333330);
INSERT INTO Appointment VALUES(13216584,100000001,101,1,'2008-04-24 10:00','2008-04-24 11:00','A');
INSERT INTO Appointment VALUES(26548913,100000002,101,2,'2008-04-24 10:00','2008-04-24 11:00','B');
INSERT INTO Appointment VALUES(36549879,100000001,102,1,'2008-04-25 10:00','2008-04-25 11:00','A');
INSERT INTO Appointment VALUES(46846589,100000004,103,4,'2008-04-25 10:00','2008-04-25 11:00','B');
INSERT INTO Appointment VALUES(59871321,100000004,NULL,4,'2008-04-26 10:00','2008-04-26 11:00','C');
INSERT INTO Appointment VALUES(69879231,100000003,103,2,'2008-04-26 11:00','2008-04-26 12:00','C');
INSERT INTO Appointment VALUES(76983231,100000001,NULL,3,'2008-04-26 12:00','2008-04-26 13:00','C');
INSERT INTO Appointment VALUES(86213939,100000004,102,9,'2008-04-27 10:00','2008-04-21 11:00','A');
INSERT INTO Appointment VALUES(93216548,100000002,101,2,'2008-04-27 10:00','2008-04-27 11:00','B');
INSERT INTO Medication VALUES(1,'Procrastin-X','X','N/A');
INSERT INTO Medication VALUES(2,'Thesisin','Foo Labs','N/A');
INSERT INTO Medication VALUES(3,'Awakin','Bar Laboratories','N/A');
INSERT INTO Medication VALUES(4,'Crescavitin','Baz Industries','N/A');
INSERT INTO Medication VALUES(5,'Melioraurin','Snafu Pharmaceuticals','N/A');
INSERT INTO Prescribes VALUES(1,100000001,1,'2008-04-24 10:47',13216584,'5');
INSERT INTO Prescribes VALUES(9,100000004,2,'2008-04-27 10:53',86213939,'10');
INSERT INTO Prescribes VALUES(9,100000004,2,'2008-04-30 16:53',NULL,'5');
INSERT INTO Block VALUES(1,1);
INSERT INTO Block VALUES(1,2);
INSERT INTO Block VALUES(1,3);
INSERT INTO Block VALUES(2,1);
INSERT INTO Block VALUES(2,2);
INSERT INTO Block VALUES(2,3);
INSERT INTO Block VALUES(3,1);
INSERT INTO Block VALUES(3,2);
INSERT INTO Block VALUES(3,3);
INSERT INTO Block VALUES(4,1);
INSERT INTO Block VALUES(4,2);
INSERT INTO Block VALUES(4,3);
INSERT INTO Room VALUES(101,'Single',1,1,0);
INSERT INTO Room VALUES(102,'Single',1,1,0);
INSERT INTO Room VALUES(103,'Single',1,1,0);
INSERT INTO Room VALUES(111,'Single',1,2,0);
INSERT INTO Room VALUES(112,'Single',1,2,1);
INSERT INTO Room VALUES(113,'Single',1,2,0);
INSERT INTO Room VALUES(121,'Single',1,3,0);
INSERT INTO Room VALUES(122,'Single',1,3,0);
INSERT INTO Room VALUES(123,'Single',1,3,0);
INSERT INTO Room VALUES(201,'Single',2,1,1);
INSERT INTO Room VALUES(202,'Single',2,1,0);
INSERT INTO Room VALUES(203,'Single',2,1,0);
INSERT INTO Room VALUES(211,'Single',2,2,0);
INSERT INTO Room VALUES(212,'Single',2,2,0);
INSERT INTO Room VALUES(213,'Single',2,2,1);
INSERT INTO Room VALUES(221,'Single',2,3,0);
INSERT INTO Room VALUES(222,'Single',2,3,0);
INSERT INTO Room VALUES(223,'Single',2,3,0);
INSERT INTO Room VALUES(301,'Single',3,1,0);
INSERT INTO Room VALUES(302,'Single',3,1,1);
INSERT INTO Room VALUES(303,'Single',3,1,0);
INSERT INTO Room VALUES(311,'Single',3,2,0);
INSERT INTO Room VALUES(312,'Single',3,2,0);
INSERT INTO Room VALUES(313,'Single',3,2,0);
INSERT INTO Room VALUES(321,'Single',3,3,1);
INSERT INTO Room VALUES(322,'Single',3,3,0);
INSERT INTO Room VALUES(323,'Single',3,3,0);
INSERT INTO Room VALUES(401,'Single',4,1,0);
INSERT INTO Room VALUES(402,'Single',4,1,1);
INSERT INTO Room VALUES(403,'Single',4,1,0);
INSERT INTO Room VALUES(411,'Single',4,2,0);
INSERT INTO Room VALUES(412,'Single',4,2,0);
INSERT INTO Room VALUES(413,'Single',4,2,0);
INSERT INTO Room VALUES(421,'Single',4,3,1);
INSERT INTO Room VALUES(422,'Single',4,3,0);
INSERT INTO Room VALUES(423,'Single',4,3,0);
INSERT INTO On_Call VALUES(101,1,1,'2008-11-04 11:00','2008-11-04 19:00');
INSERT INTO On_Call VALUES(101,1,2,'2008-11-04 11:00','2008-11-04 19:00');
INSERT INTO On_Call VALUES(102,1,3,'2008-11-04 11:00','2008-11-04 19:00');
INSERT INTO On_Call VALUES(103,1,1,'2008-11-04 19:00','2008-11-05 03:00');
INSERT INTO On_Call VALUES(103,1,2,'2008-11-04 19:00','2008-11-05 03:00');
INSERT INTO On_Call VALUES(103,1,3,'2008-11-04 19:00','2008-11-05 03:00');
INSERT INTO Stay VALUES(3215,100000001,111,'2008-05-01','2008-05-04');
INSERT INTO Stay VALUES(3216,100000003,123,'2008-05-03','2008-05-14');
INSERT INTO Stay VALUES(3217,100000004,112,'2008-05-02','2008-05-03');
INSERT INTO Undergoes VALUES(100000001,6,3215,'2008-05-02',3,101);
INSERT INTO Undergoes VALUES(100000001,2,3215,'2008-05-03',7,101);
INSERT INTO Undergoes VALUES(100000004,1,3217,'2008-05-07',3,102);
INSERT INTO Undergoes VALUES(100000004,5,3217,'2008-05-09',6,NULL);
INSERT INTO Undergoes VALUES(100000001,7,3217,'2008-05-10',7,101);
INSERT INTO Undergoes VALUES(100000004,4,3217,'2008-05-13',3,103);
INSERT INTO Trained_In VALUES(3,1,'2008-01-01','2008-12-31');
INSERT INTO Trained_In VALUES(3,2,'2008-01-01','2008-12-31');
INSERT INTO Trained_In VALUES(3,5,'2008-01-01','2008-12-31');
INSERT INTO Trained_In VALUES(3,6,'2008-01-01','2008-12-31');
INSERT INTO Trained_In VALUES(3,7,'2008-01-01','2008-12-31');
INSERT INTO Trained_In VALUES(6,2,'2008-01-01','2008-12-31');
INSERT INTO Trained_In VALUES(6,5,'2007-01-01','2007-12-31');
INSERT INTO Trained_In VALUES(6,6,'2008-01-01','2008-12-31');
INSERT INTO Trained_In VALUES(7,1,'2008-01-01','2008-12-31');
INSERT INTO Trained_In VALUES(7,2,'2008-01-01','2008-12-31');
INSERT INTO Trained_In VALUES(7,3,'2008-01-01','2008-12-31');
INSERT INTO Trained_In VALUES(7,4,'2008-01-01','2008-12-31');
INSERT INTO Trained_In VALUES(7,5,'2008-01-01','2008-12-31');
INSERT INTO Trained_In VALUES(7,6,'2008-01-01','2008-12-31');
INSERT INTO Trained_In VALUES(7,7,'2008-01-01','2008-12-31');
""".split('\n')
for i,line in enumerate(data):
if line:
cur.execute(line.strip())
con.commit()
tables = pd.read_sql("""SELECT *
FROM sqlite_master
WHERE type='table';""", con)
tables
type | name | tbl_name | rootpage | sql | |
---|---|---|---|---|---|
0 | table | Physician | Physician | 2 | CREATE TABLE Physician (\n EmployeeID INTEGER PRIMARY KEY NOT NULL,\n Name TEXT NOT NULL,\n P... |
1 | table | Department | Department | 3 | CREATE TABLE Department (\n DepartmentID INTEGER PRIMARY KEY NOT NULL,\n Name TEXT NOT NULL,\n... |
2 | table | Affiliated_With | Affiliated_With | 4 | CREATE TABLE Affiliated_With (\n Physician INTEGER NOT NULL\n CONSTRAINT fk_Physician_Employ... |
3 | table | Procedure | Procedure | 6 | CREATE TABLE Procedure (\n Code INTEGER PRIMARY KEY NOT NULL,\n Name TEXT NOT NULL,\n Cost RE... |
4 | table | Trained_In | Trained_In | 7 | CREATE TABLE Trained_In (\n Physician INTEGER NOT NULL\n CONSTRAINT fk_Physician_EmployeeID ... |
5 | table | Patient | Patient | 9 | CREATE TABLE Patient (\n SSN INTEGER PRIMARY KEY NOT NULL,\n Name TEXT NOT NULL,\n Address TE... |
6 | table | Nurse | Nurse | 10 | CREATE TABLE Nurse (\n EmployeeID INTEGER PRIMARY KEY NOT NULL,\n Name TEXT NOT NULL,\n Posit... |
7 | table | Appointment | Appointment | 11 | CREATE TABLE Appointment (\n AppointmentID INTEGER PRIMARY KEY NOT NULL,\n Patient INTEGER NOT... |
8 | table | Medication | Medication | 12 | CREATE TABLE Medication (\n Code INTEGER PRIMARY KEY NOT NULL,\n Name TEXT NOT NULL,\n Brand ... |
9 | table | Prescribes | Prescribes | 13 | CREATE TABLE Prescribes (\n Physician INTEGER NOT NULL\n CONSTRAINT fk_Physician_EmployeeID ... |
10 | table | Block | Block | 15 | CREATE TABLE Block (\n Floor INTEGER NOT NULL,\n Code INTEGER NOT NULL,\n PRIMARY KEY(Floor, ... |
11 | table | Room | Room | 17 | CREATE TABLE Room (\n Number INTEGER PRIMARY KEY NOT NULL,\n Type TEXT NOT NULL,\n BlockFloor... |
12 | table | On_Call | On_Call | 18 | CREATE TABLE On_Call (\n Nurse INTEGER NOT NULL\n CONSTRAINT fk_Nurse_EmployeeID REFERENCES ... |
13 | table | Stay | Stay | 21 | CREATE TABLE Stay (\n StayID INTEGER PRIMARY KEY NOT NULL,\n Patient INTEGER NOT NULL\n CON... |
14 | table | Undergoes | Undergoes | 23 | CREATE TABLE Undergoes (\n Patient INTEGER NOT NULL\n CONSTRAINT fk_Patient_SSN REFERENCES P... |
for t in tables['name']:
print(t)
display(pd.read_sql(f'select * from {t} limit 1',con))
Physician
EmployeeID | Name | Position | SSN | |
---|---|---|---|---|
0 | 1 | John Dorian | Staff Internist | 111111111 |
Department
DepartmentID | Name | Head | |
---|---|---|---|
0 | 1 | General Medicine | 4 |
Affiliated_With
Physician | Department | PrimaryAffiliation | |
---|---|---|---|
0 | 1 | 1 | 1 |
Procedure
Code | Name | Cost | |
---|---|---|---|
0 | 1 | Reverse Rhinopodoplasty | 1500.0 |
Trained_In
Physician | Treatment | CertificationDate | CertificationExpires | |
---|---|---|---|---|
0 | 3 | 1 | 2008-01-01 | 2008-12-31 |
Patient
SSN | Name | Address | Phone | InsuranceID | PCP | |
---|---|---|---|---|---|---|
0 | 100000001 | John Smith | 42 Foobar Lane | 555-0256 | 68476213 | 1 |
Nurse
EmployeeID | Name | Position | Registered | SSN | |
---|---|---|---|---|---|
0 | 101 | Carla Espinosa | Head Nurse | 1 | 111111110 |
Appointment
AppointmentID | Patient | PrepNurse | Physician | Start | End | ExaminationRoom | |
---|---|---|---|---|---|---|---|
0 | 13216584 | 100000001 | 101 | 1 | 2008-04-24 10:00 | 2008-04-24 11:00 | A |
Medication
Code | Name | Brand | Description | |
---|---|---|---|---|
0 | 1 | Procrastin-X | X | N/A |
Prescribes
Physician | Patient | Medication | Date | Appointment | Dose | |
---|---|---|---|---|---|---|
0 | 1 | 100000001 | 1 | 2008-04-24 10:47 | 13216584 | 5 |
Block
Floor | Code | |
---|---|---|
0 | 1 | 1 |
Room
Number | Type | BlockFloor | BlockCode | Unavailable | |
---|---|---|---|---|---|
0 | 101 | Single | 1 | 1 | 0 |
On_Call
Nurse | BlockFloor | BlockCode | Start | End | |
---|---|---|---|---|---|
0 | 101 | 1 | 1 | 2008-11-04 11:00 | 2008-11-04 19:00 |
Stay
StayID | Patient | Room | Start | End | |
---|---|---|---|---|---|
0 | 3215 | 100000001 | 111 | 2008-05-01 | 2008-05-04 |
Undergoes
Patient | Procedure | Stay | Date | Physician | AssistingNurse | |
---|---|---|---|---|---|---|
0 | 100000001 | 6 | 3215 | 2008-05-02 | 3 | 101 |
q = """
SELECT Name
FROM Physician
WHERE EmployeeID IN
(
SELECT Physician FROM Undergoes U WHERE NOT EXISTS
(
SELECT * FROM Trained_In
WHERE Treatment = Procedure
AND Physician = U.Physician
)
)
"""
pd.read_sql(q,con)
Name | |
---|---|
0 | Christopher Turk |
q = """
SELECT P.Name FROM
Physician AS P,
(SELECT Physician, Procedure FROM Undergoes
EXCEPT
SELECT Physician, Treatment FROM Trained_in) AS Pe
WHERE P.EmployeeID=Pe.Physician
"""
pd.read_sql(q,con)
Name | |
---|---|
0 | Christopher Turk |
q = """
SELECT Name
FROM Physician
WHERE EmployeeID IN
(
SELECT Undergoes.Physician
FROM Undergoes
LEFT JOIN Trained_In
ON Undergoes.Physician=Trained_In.Physician
AND Undergoes.Procedure=Trained_In.Treatment
WHERE Treatment IS NULL
)
"""
pd.read_sql(q,con)
Name | |
---|---|
0 | Christopher Turk |
q = """
SELECT P.Name AS Physician, Pr.Name AS Procedure, U.Date, Pt.Name AS Patient
FROM Physician P, Undergoes U, Patient Pt, Procedure Pr
WHERE U.Patient = Pt.SSN
AND U.Procedure = Pr.Code
AND U.Physician = P.EmployeeID
AND NOT EXISTS
(
SELECT * FROM Trained_In T
WHERE T.Treatment = U.Procedure
AND T.Physician = U.Physician
)
"""
pd.read_sql(q,con)
Physician | Procedure | Date | Patient | |
---|---|---|---|---|
0 | Christopher Turk | Complete Walletectomy | 2008-05-13 | Dennis Doe |
q = """
SELECT P.Name,Pr.Name,U.Date,Pt.Name FROM
Physician AS P,
Procedure AS Pr,
Undergoes AS U,
Patient AS Pt,
(SELECT Physician, Procedure FROM Undergoes
EXCEPT
SELECT Physician, Treatment FROM Trained_in) AS Pe
WHERE P.EmployeeID=Pe.Physician AND Pe.Procedure=Pr.Code AND Pe.Physician=U.Physician AND Pe.Procedure=U.Procedure AND U.Patient=Pt.SSN
"""
pd.read_sql(q,con)
Name | Name | Date | Name | |
---|---|---|---|---|
0 | Christopher Turk | Complete Walletectomy | 2008-05-13 | Dennis Doe |
q = """
SELECT Name
FROM Physician
WHERE EmployeeID IN
(
SELECT Physician FROM Undergoes U
WHERE Date >
(
SELECT CertificationExpires
FROM Trained_In T
WHERE T.Physician = U.Physician
AND T.Treatment = U.Procedure
)
)
"""
pd.read_sql(q,con)
Name | |
---|---|
0 | Todd Quinlan |
q = """
SELECT P.Name FROM
Physician AS P,
Trained_In T,
Undergoes AS U
WHERE T.Physician=U.Physician AND T.Treatment=U.Procedure AND U.Date>T.CertificationExpires AND P.EmployeeID=U.Physician
"""
pd.read_sql(q,con)
Name | |
---|---|
0 | Todd Quinlan |
q = """
SELECT P.Name AS Physician, Pr.Name AS Procedure, U.Date, Pt.Name AS Patient, T.CertificationExpires
FROM Physician P, Undergoes U, Patient Pt, Procedure Pr, Trained_In T
WHERE U.Patient = Pt.SSN
AND U.Procedure = Pr.Code
AND U.Physician = P.EmployeeID
AND Pr.Code = T.Treatment
AND P.EmployeeID = T.Physician
AND U.Date > T.CertificationExpires
"""
pd.read_sql(q,con)
Physician | Procedure | Date | Patient | CertificationExpires | |
---|---|---|---|---|---|
0 | Todd Quinlan | Obfuscated Dermogastrotomy | 2008-05-09 | Dennis Doe | 2007-12-31 |
q = """
SELECT Pt.Name AS Patient, Ph.Name AS Physician, N.Name AS Nurse, A.Start, A.End, A.ExaminationRoom, PhPCP.Name AS PCP
FROM Patient Pt, Physician Ph, Physician PhPCP, Appointment A LEFT JOIN Nurse N ON A.PrepNurse = N.EmployeeID
WHERE A.Patient = Pt.SSN
AND A.Physician = Ph.EmployeeID
AND Pt.PCP = PhPCP.EmployeeID
AND A.Physician <> Pt.PCP;
"""
pd.read_sql(q,con)
Patient | Physician | Nurse | Start | End | ExaminationRoom | PCP | |
---|---|---|---|---|---|---|---|
0 | Dennis Doe | Percival Cox | Paul Flowers | 2008-04-25 10:00 | 2008-04-25 11:00 | B | Christopher Turk |
1 | Dennis Doe | Percival Cox | None | 2008-04-26 10:00 | 2008-04-26 11:00 | C | Christopher Turk |
2 | John Smith | Christopher Turk | None | 2008-04-26 12:00 | 2008-04-26 13:00 | C | John Dorian |
3 | Dennis Doe | Molly Clock | Laverne Roberts | 2008-04-27 10:00 | 2008-04-21 11:00 | A | Christopher Turk |
q = """
SELECT * FROM Undergoes U
WHERE Patient <>
(
SELECT Patient FROM Stay S
WHERE U.Stay = S.StayID
);
"""
pd.read_sql(q,con)
Patient | Procedure | Stay | Date | Physician | AssistingNurse | |
---|---|---|---|---|---|---|
0 | 100000001 | 7 | 3217 | 2008-05-10 | 7 | 101 |
q = """
SELECT N.Name FROM Nurse N
WHERE EmployeeID IN
(
SELECT OC.Nurse FROM On_Call OC, Room R
WHERE OC.BlockFloor = R.BlockFloor
AND OC.BlockCode = R.BlockCode
AND R.Number = 123
);
"""
pd.read_sql(q,con)
Name | |
---|---|
0 | Laverne Roberts |
1 | Paul Flowers |
q = """
SELECT ExaminationRoom, COUNT(AppointmentID) AS Number FROM Appointment
GROUP BY ExaminationRoom;
"""
pd.read_sql(q,con)
ExaminationRoom | Number | |
---|---|---|
0 | A | 3 |
1 | B | 3 |
2 | C | 3 |
q = """
SELECT Pt.Name, PhPCP.Name FROM Patient Pt, Physician PhPCP
WHERE Pt.PCP = PhPCP.EmployeeID
AND EXISTS
(
SELECT * FROM Prescribes Pr
WHERE Pr.Patient = Pt.SSN
AND Pr.Physician = Pt.PCP
)
AND EXISTS
(
SELECT * FROM Undergoes U, Procedure Pr
WHERE U.Procedure = Pr.Code
AND U.Patient = Pt.SSN
AND Pr.Cost > 5000
)
AND 2 <=
(
SELECT COUNT(A.AppointmentID) FROM Appointment A, Nurse N
WHERE A.PrepNurse = N.EmployeeID
AND N.Registered = 1
)
AND NOT Pt.PCP IN
(
SELECT Head FROM Department
);
"""
pd.read_sql(q,con)
Name | Name | |
---|---|---|
0 | John Smith | John Dorian |
time_taken = time.time() - time_start_notebook
h,m = divmod(time_taken,60*60)
print('Time taken to run whole notebook: {:.0f} hr '\
'{:.0f} min {:.0f} secs'.format(h, *divmod(m,60)))
Time taken to run whole notebook: 0 hr 0 min 1 secs