You are on page 1of 4

Creating Table PATIENTS:

========================

drop table PATIENTS;

Create table PATIENTS


(PatientID number(5) constraint PATIENTS_C1_PatientID primary key,
Patientno number(5),
Initials varchar2(5));

Insert into PATIENTS values(1,1,'ABC');


Insert into PATIENTS values(2,3,'DEF');
Insert into PATIENTS values(3,4,'HIJ');

select * from PATIENTS;

Creating table VISITS:


======================

drop table VISITS;

Create table VISITS


(visitid number(5) constraint VISITS_C1_visitid primary key,
Patientid number(5),
VisitNo number(5),
VisitDate date);

alter table VISITS add constraint VISITS_C2_PatientID Foreign Key(PatientID)


References PATIENTS(PatientID);

insert into VISITS values(1,1,1,'12-JAN-2005');


insert into VISITS values(2,1,2,'23-JAN-2005');
insert into VISITS values(3,1,3,'28-JAN-2005');
insert into VISITS values(4,2,1,'01-FEB-2005');
insert into VISITS values(5,2,2,'08-FEB-2005');
insert into VISITS values(6,3,1,'03-FEB-2005');
insert into VISITS values(7,3,2,'08-FEB-2005');

select * from VISITS;

Creating table VITALSIGNS:


==========================

drop table VITALSIGNS;

Create table VITALSIGNS


(VisitId number(5) constraint VITALSIGNs_c1_VisitId primary key,
Systolic number(5),
Diastolic number(5));

alter table VITALSIGNS add constraint VITALSIGNS_C2_VisitId foreign key(VisitId)


references VISIT(VisitId);

insert into VITALSIGNS Values(1,120,80);


insert into VITALSIGNS values(2,125,84);
insert into VITALSIGNS values(3,130,90);
insert into VITALSIGNS values(4,145,101);
insert into VITALSIGNS values(5,139,96);
insert into VITALSIGNS values(6,142,97);
insert into VITALSIGNS values(7,145,100);

select * from VITALSIGNS;

Creating table ADVERSEEVENTS:


=============================

drop table ADVERSEEVENTS;

Create table ADVERSEEVENTS


(VisitId number(5),
AE Varchar2(20),
Med varchar2(10));

alter table ADVERSEEVENTS add constraints ADVERSEEVENTS_C2_VisitID foreign


Key(VisitId) references VITALSIGNS(VisitId);

Insert into ADVERSEEVENTS values(1,'Headache','Aspirin');


insert into ADVERSEEVENTS(VisitId,AE) values(1,'Nausea');
insert into ADVERSEEVENTS values(3,'Headache','Tylenol');
insert into ADVERSEEVENTS(VisitId,AE) values(4,'High BP');
insert into ADVERSEEVENTS values(4,'Heartburn','Zantac');
insert into ADVERSEEVENTS values(7,'High BP','Norvasc');

select * from ADVERSEEVENTS;

Creating table MEDS:


====================

drop table MEDS;

Create table MEDS


(VisitId number(5) constraint MEDS_C1_VisitId primary Key,
Med varchar2(10),
MedDate date,
Takenfor varchar2(10));

alter table MEDS add constraint MEDS_C2_VisitId Foreign Key(VisitId) references


VITALSIGNS(VisitId);

insert into MEDS(VisitId,Med,MedDate) values(1,'Aspirin','11-JAN-2005');


insert into MEDS(VisitId,Med,MedDate) values(3,'Tylenol','26-JAN-2005');
insert into MEDS(VisitId,Med,MedDate) values(4,'Zantac','01-FEB-2005');
insert into MEDS(VisitId,Med,MedDate) values(6,'Aleve','01-FEB-2005');

select * from MEDS;

REQUIREMENT:
============

From above database table data, you are requested to write queries / solutions for
below asks:

1. Write the SQL to produce the following report on the vital signs data.
Order by patient number and visit number.
PatientNo VisitNo VisitDate Systolic
Diastolic

-------------select
PATIENTS.Patientno,VISITS.Visitno,VISITS.VisitDate,VITALSIGNS.Systolic,VITALSIGNS.D
iastolic from PATIENTS Inner Join VISITS on PATIENTS.PatientId=VISITS.PatientID

Inner join VITALSIGNS on VISITS.VisitId=VITALSIGNS.VisitId;

2. List the patient number and initials for every patient that never had a headache

-------------select PATIENTS.Patientno,PATIENTS.Initials,ADVERSEEVENTS.AE from


PATIENTS Inner join VISITS on PATIENTS.PatientID=VISITS.PatientID

Inner join ADVERSEEVENTS on VISITS.VisitId=ADVERSEEVENTS.VisitId and


ADVERSEEVENTS.AE != 'Headache';

3.Produce the following report, showing each adverse event and the details of the
medication taken for that adverse event. Include all adverse events, even those for
which no medication was taken.

PatientNo AE Med
MedDate

-------------select
PATIENTS.Patientno,VISITS.PatientId,ADVERSEEVENTS.*,MEDS.MedDate from PATIENTS
Inner Join VISITS on PATIENTS.PATIENTID=VISITS.PATIENTID

Inner Join ADVERSEEVENTS on VISITS.VisitId=ADVERSEEVENTS.VisitId

Inner Join MEDS on VISITS.VisitId=MEDS.VisitId;

4.List the distinct set of medications taken in the study. Beware � this is not as
trivial as it looks. Extra credit for being careful.

--------------dint get this

5.List the average systolic blood pressure (Sys) for all patients who ever had an
adverse event of �High BP�

-------------select avg(VITALSIGNS.Systolic),ADVERSEEVENTS.AE from VITALSIGNS Inner


Join ADVERSEEVENTS on VITALSIGNS.VisitId=ADVERSEEVENTS.VisitId and ADVERSEEVENTS.AE
!='Headache' group by ADVERSEEVENTS.AE;

6. List all patients who had a headache more than once.

-------------select
PATIENTS.Patientid,PATIENTS.PatientNo,PATIENTS.Initials,count(VISITS.PatientId)
from PATIENTS Inner Join VISITS on PATIENTS.PatientId=VISITS.PatientId
Inner Join ADVERSEEVENTS on VISITS.VisitId=ADVERSEEVENTS.VisitId and
ADVERSEEVENTS.AE = 'Headache' group by
PATIENTS.Patientid,PATIENTS.PatientNo,PATIENTS.Initials

having count(VISITS.PatientId)>1;

You might also like