Sei sulla pagina 1di 19

Reynaldi – 000000 25047

/* RFS99 */ SCREENSHOTS SETELAH


sqlplus KODINGAN DI BAGIAN
sys / as sysdba BAWAH
startup
con hr/hr
alter SESSION set NLS_DATE_FORMAT = 'YYYY-MM-DD' ;

CREATE TABLE MsMedicineType(


MedicineTypeID varchar(10) ,
MedicineTypeName varchar(50) NOT NULL,
CONSTRAINT MedicineTypeID_PK PRIMARY KEY (MedicineTypeID) );

CREATE TABLE MsPatient(


PatientID varchar(10) ,
PatientName varchar(50) NOT NULL,
PatientAddress varchar(50) NOT NULL,
PatientPhone varchar(20) UNIQUE,
PatientBirthDate DATE NOT NULL,
CONSTRAINT PatientID_PK PRIMARY KEY (PatientID)
);

CREATE TABLE MsDoctor(


DoctorID varchar(10) ,
DoctorName varchar(50) NOT NULL,
DoctorAddress varchar(50) NOT NULL,
DoctorPhone varchar(20) UNIQUE,
DoctorBirthDate DATE NOT NULL,
CONSTRAINT DoctorID_PK PRIMARY KEY (DoctorID) );
Reynaldi – 000000 25047

CREATE TABLE MsMedicine(


MedicineID varchar(10) ,
MedicineTypeID varchar(10) ,
MedicineName varchar(50) NOT NULL,
MedicinePrice number(9,2) NOT NULL,
MedicineStock number(12) NOT NULL,
CONSTRAINT MedicineID_PK PRIMARY KEY (MedicineID),
CONSTRAINT Med_Type_ID_FK FOREIGN KEY (MedicineTypeID)
REFERENCES MsMedicineType (MedicineTypeID) );

CREATE TABLE TransactionHeader(


TransactionID varchar(10) ,
DoctorID varchar(10) ,
PatientID varchar(10) ,
TransactionDate DATE ,
CONSTRAINT TransactionID_PK PRIMARY KEY (TransactionID),
CONSTRAINT Doctor_ID_FK FOREIGN KEY (DoctorID)
REFERENCES MsDoctor (DoctorID),
CONSTRAINT Patient_ID_FK FOREIGN KEY (PatientID)
REFERENCES MsPatient (PatientID) );

CREATE TABLE TransactionDetail(


TransactionID varchar(10) ,
MedicineID varchar(10) ,
Quantity number(10) NOT NULL,
CONSTRAINT TransactionID_MedicineID_PK PRIMARY KEY(TransactionID,MedicineID),
CONSTRAINT Transaction_ID_FK FOREIGN KEY (TransactionID)
REFERENCES TransactionHeader (TransactionID),
CONSTRAINT Medicine_ID_FK FOREIGN KEY (MedicineID)
REFERENCES MsMedicine (MedicineID) );
Reynaldi – 000000 25047

/* NO 1 */
SELECT MAX(Quantity) as "MaxQuantity", MIN(Quantity) as
"MinQuantity",trunc(AVG(Quantity),2) as "AvgQuantity"
FROM TransactionDetail;

/* NO 2 */
SELECT MedicineTypeID, MedicineTypeName,
COUNT(MedicineID) as "MedicineCount"
FROM MsMedicine NATURAL JOIN MsMedicineType
GROUP BY MedicineTypeID, MedicineTypeName
ORDER BY 1;

/* NO 3 */
SELECT DoctorID, DoctorName, COUNT(DoctorID) as "TransactionCount"
FROM TransactionHeader NATURAL JOIN MsDoctor
WHERE EXTRACT(month from TransactionDate) < 7
GROUP BY DoctorID,DoctorName
ORDER BY 1;

/* NO 4 */
SELECT m.PatientID, m.PatientName, SUM(Quantity) as "TotalMedicineBough"
FROM TransactionDetail td JOIN TransactionHeader th
ON( th.TransactionID = td.TransactionID)
JOIN MsPatient m
ON(th.PatientID = m.PatientID)
WHERE td.MedicineID = 'ME008' AND Length(PatientName) > 7
GROUP BY m.PatientID, m.PatientName
ORDER BY 1;
Reynaldi – 000000 25047

/* NO 5 */
SELECT TO_CHAR(TransactionDate, 'YYYY') as "Year", MAX(Quantity)
FROM TransactionDetail NATURAL JOIN TransactionHeader
WHERE DoctorID = 'DC001'
GROUP BY TO_CHAR(TransactionDate, 'YYYY')
ORDER BY 1;

/* NO 6 */
SELECT PatientID, PatientName, COUNT(PatientID) as "TransactionCount"
FROM TransactionHeader NATURAL JOIN MsPatient
WHERE PatientPhone NOT LIKE '021%' AND PatientAddress NOT LIKE '% % %'
GROUP BY PatientID, PatientName
ORDER BY 1;

/* NO 7 */
SELECT md.DoctorID, md.DoctorName, TO_CHAR(md.DoctorBirthDate, 'dd Mon YYYY') as
"DoctorBirthday", SUM(td.Quantity) as "TotalMedicineSold"
FROM TransactionDetail td
JOIN TransactionHeader th
ON(th.TransactionID = td.TransactionID)
JOIN MsDoctor md
ON(th.DoctorID = md.DoctorID)
WHERE LENGTH(md.DoctorName) > 14
GROUP BY md.DoctorID, md.DoctorName, TO_CHAR(md.DoctorBirthDate, 'dd Mon YYYY');
Reynaldi – 000000 25047

/* NO 8 */
SELECT m.MedicineID, m.MedicineName, td.Quantity * m.MedicinePrice as "TotalProfit"
FROM MsMedicine m
JOIN TransactionDetail td
ON(td.MedicineID = m.MedicineID)
JOIN TransactionHeader th
ON(td.TransactionID = th.TransactionID)
WHERE TO_CHAR(th.TransactionDate, 'dd') BETWEEN 10 AND 20
GROUP BY m.MedicineID, m.MedicineName,td.Quantity * m.MedicinePrice;

/* NO 9 */
SELECT RPAD(th.TransactionID,5,' ') as "TransactionID", SUM(td.Quantity) as "TotalMedicine"
FROM TransactionDetail td
JOIN TransactionHeader th
ON(th.TransactionID = td.TransactionID)
WHERE TO_CHAR(th.TransactionDate, 'YYYY') = 2012
GROUP BY th.TransactionID
ORDER BY 1;
Reynaldi – 000000 25047

/* NO 10 */
-Tipe 1-
SELECT th.TransactionID, md.DoctorID, mp.PatientID, COUNT(td.MedicineID)
FROM TransactionDetail td
JOIN TransactionHeader th
ON(th.TransactionID = td.TransactionID)
JOIN MsDoctor md
ON(th.DoctorID = md.DoctorID)
JOIN MsPatient mp
ON(th.PatientID = mp.PatientID)
WHERE TO_NUMBER(TRIM('A' FROM TRIM('P' FROM mp.PatientID))) > 4
AND MOD(TO_NUMBER(TRIM('A' FROM TRIM('P' FROM mp.PatientID))),4) = 0
GROUP BY th.TransactionID, md.DoctorID, mp.PatientID
ORDER BY 2;

-Tipe 2-
SELECT th.TransactionID, md.DoctorID, mp.PatientID, COUNT(td.MedicineID)
FROM TransactionDetail td
JOIN TransactionHeader th
ON(th.TransactionID = td.TransactionID)
JOIN MsDoctor md
ON(th.DoctorID = md.DoctorID)
JOIN MsPatient mp
ON(th.PatientID = mp.PatientID)
WHERE
MOD(TO_NUMBER(TRIM('A' FROM TRIM('P' FROM mp.PatientID))),4) = 0
GROUP BY th.TransactionID, md.DoctorID, mp.PatientID
ORDER BY 2;
Reynaldi – 000000 25047

/* NO 11 */
SELECT m.MedicineID, m.MedicineName
FROM MsMedicine m
JOIN MsMedicineType mt
ON(m.MedicineTypeID = mt.MedicineTypeID)
WHERE mt.MedicineTypeName = 'Balm'
GROUP BY m.MedicineID, m.MedicineName
ORDER BY 1;

/* NO 12 */
SELECT md.DoctorID, md.DoctorName, md.DoctorPhone,
TO_CHAR(md.DoctorBirthDate, 'YYYY') DoctorBirthYear
FROM MsDoctor md, TransactionHeader th
GROUP BY md.DoctorID, md.DoctorName, md.DoctorPhone,
TO_CHAR(md.DoctorBirthDate, 'YYYY')
MINUS
SELECT md.DoctorID, md.DoctorName, md.DoctorPhone,
TO_CHAR(md.DoctorBirthDate, 'YYYY') DoctorBirthYear
FROM MsDoctor md, TransactionHeader th
WHERE md.DoctorID = th.DoctorID
GROUP BY md.DoctorID, md.DoctorName, md.DoctorPhone,
TO_CHAR(md.DoctorBirthDate, 'YYYY');

/*SELECT md.DoctorID, md.DoctorName, md.DoctorPhone,


TO_CHAR(md.DoctorBirthDate,'YYYY') as "DoctorBirthYear"
FROM TransactionHeader th
JOIN MsDoctor md
ON(th.DoctorID = md.DoctorID)
WHERE md.DoctorID NOT IN
(SELECT md.DoctorID
Reynaldi – 000000 25047

FROM TransactionHeader th
JOIN MsDoctor md
ON(th.DoctorID = md.DoctorID));*/

/* NO 13 */
SELECT m.MedicineName, mt.MedicineTypeName, m.MedicinePrice FROM
MsMedicine m
JOIN MsMedicineType mt
ON mt.MedicineTypeID = m.MedicineTypeID
JOIN TransactionDetail d
ON d.MedicineID = m.MedicineID
JOIN TransactionHeader h
ON d.TransactionID = h.TransactionID
WHERE EXTRACT(Month from h.TransactionDate) = 12
ORDER BY m.MedicinePrice DESC;

/* NO 14 */
SELECT mm.MedicineID, mm.MedicineName,
'Rp. '|| mm.MedicinePrice as "MedicinePrice"
FROM MsMedicine mm
JOIN TransactionDetail td
ON mm.MedicineID = td.MedicineID
JOIN TransactionHeader th
ON td.TransactionID = th.TransactionID
JOIN MsDoctor md
ON th.DoctorID = md.DoctorID
WHERE th.DoctorID NOT LIKE 'DC001'
GROUP BY mm.MedicineID, mm.MedicineName, mm.MedicinePrice
ORDER BY 1;
Reynaldi – 000000 25047

/* NO 15 */
SELECT DISTINCT p.PatientID, p.PatientName,
TO_CHAR(p.PatientBirthDate, 'yyyy') as "PatientBirthYear"
FROM MsPatient p
JOIN TransactionHeader th
ON(th.PatientID = p.PatientID)
JOIN MsDoctor d
ON(th.DoctorID = d.DoctorID)
WHERE TO_CHAR(p.PatientBirthDate, 'yyyy-mm-dd') > TO_CHAR(d.DoctorBirthDate, 'yyyy-mm-
dd')
ORDER BY 1;

/* NO 16 */
SELECT h.transactionID, TO_CHAR(h.TransactionDate,'yyyy.mm.dd')
as "TransactionDate"
FROM transactionHeader h
JOIN transactionDetail d
ON (h.TransactionID = d.TransactionID)
JOIN MsMedicine m
ON m.MedicineID = d.MedicineID
JOIN MsMedicineType mt
ON (m.MedicineTypeID = mt.MedicineTypeID)
WHERE mt.MedicineTypeName <> 'Syrup' and m.MedicinePrice > 15000
GROUP BY h.TransactionID, h.TransactionDate
ORDER BY 1;

/* NO 17 */
SELECT PatientID, PatientName, REPLACE(PatientAddress,'Street',' ')PatientRoadAddress
FROM MsPatient
WHERE LENGTH(PatientName) > LENGTH(REPLACE(PatientAddress,'Street',' '));
Reynaldi – 000000 25047

/* NO 18 */
SELECT p.PatientName, TO_CHAR(p.PatientBirthDate, 'dd/mm/yy') as "PatientBirthDate",
p.PatientAddress
FROM MsPatient p
JOIN TransactionHeader h
ON(p.PatientID = h.PatientID)
JOIN MsDoctor d
ON(d.DoctorID = h.DoctorID)
WHERE TO_CHAR(h.TransactionDate, 'dd') BETWEEN 15 AND 25
AND d.DoctorName LIKE '%j%'
OR d.DoctorName LIKE '%k%';

/* NO 19 */
SELECT p.PatientID, p.PatientName,
SUBSTR(UPPER(p.PatientName),1,2) as "INITIAL",
p.PatientBirthDate, COUNT(*)|| ' Transaction(s)'
FROM MsPatient p
JOIN TransactionHeader h
ON(h.PatientID = p.PatientID)
WHERE p.PatientID = h.PatientID
GROUP BY p.PatientID, p.PatientName,
SUBSTR(UPPER(p.PatientName),1,2), p.PatientBirthDate
ORDER BY p.PatientID;

/* NO 20 */
SELECT DoctorID, DoctorName, TO_CHAR(DoctorBirthDate, 'Mon dd yyyy') as
"DoctorBirthDate"
FROM MsDoctor
WHERE TO_CHAR(DoctorBirthDate, 'yyyy') = '1994'
OR TO_CHAR(DoctorBirthDate, 'yyyy') = '1954';
Reynaldi – 000000 25047

/*SELECT DoctorID, DoctorName, DoctorPhone, DoctorBirthDate


FROM MsDoctor
WHERE DoctorBirthDate = MAX(TO_NUMBER(DoctorBirthDate)) OR DoctorBirthDate =
MIN(TO_NUMBER(DoctorBirthDate))
ORDER BY 1;*/
Reynaldi – 000000 25047

SCREENSHOTS

NO 1

NO 2

NO 3
Reynaldi – 000000 25047

NO 4

NO 5

NO 6
Reynaldi – 000000 25047

NO 7

NO 8

NO 9
Reynaldi – 000000 25047

NO 10_1

NO 10_2

NO 11
Reynaldi – 000000 25047

NO 12

NO 13
Reynaldi – 000000 25047

NO 14

NO 15
Reynaldi – 000000 25047

NO 16

NO 17
Reynaldi – 000000 25047

NO 18

NO 19

NO 20

Potrebbero piacerti anche