Costliest Hospital Stay

A hospital records stays and the treatments given during them.

patients(tax_code, first_name, last_name, sex, birth_date)
treatments(code, name, cost)
stays(patient, start_date, end_date)                 -- end_date NULL: still in hospital
stay_treatments(patient, stay_start, treatment)

Write a procedure costliest_stay(p_from, p_to) that finds the stay within the period with the highest cost F, where days = end_date - start_date and

  • F = 20 + sum of treatment costs − days × 2, if the stay lasted less than 7 days;
  • F = 15 + sum of treatment costs − days × 1.5 otherwise.

Print the patient, the stay and its treatments. Stays not yet finished do not count.

Run schema.sql (tables and example data), then solution.sql.

Example

costliest_stay(DATE '2026-01-01', DATE '2026-06-30'). The data has a 3-day stay with F = 384, a 10-day stay with F = 1,240, a more expensive stay of a patient still in hospital, and a stay outside the period:

Patient: Laura Bianchi (BNCLRA75B41F205X)
Stay: 2026-04-10 to 2026-04-20, 10 days, F = 1240
  Surgery: 1200
  Blood test: 20
  Blood test: 20

Approach

Compute F in the cursor's query: join stays to their treatments, group by stay, and write the formula as a CASE on the duration. Ordering by F descending puts the answer in the first row, so the procedure fetches one row and closes the cursor, instead of looping over every stay and keeping a running maximum.

Only then, for that one stay, read the patient and list the treatments. Joining patient data into the main query would carry it for every stay just to use it once.

end_date IS NOT NULL excludes the open stay; without it, end_date - start_date is NULL, and so is F, which sorts first in a descending Oracle sort.

Lesson: PL/SQL cursors.

Solutions

solution.sql

SET SERVEROUTPUT ON FORMAT WRAPPED
SET FEEDBACK OFF
SET LINESIZE 120 PAGESIZE 100

CREATE OR REPLACE PROCEDURE costliest_stay(p_from DATE, p_to DATE) IS
    CURSOR c_stays IS
        SELECT s.patient, s.start_date, s.end_date,
               s.end_date - s.start_date AS days,
               CASE WHEN s.end_date - s.start_date < 7
                    THEN 20 + sum(t.cost) - (s.end_date - s.start_date) * 2
                    ELSE 15 + sum(t.cost) - (s.end_date - s.start_date) * 1.5
               END AS f
        FROM stays s
        JOIN stay_treatments st ON st.patient = s.patient AND st.stay_start = s.start_date
        JOIN treatments t       ON t.code = st.treatment
        WHERE s.end_date IS NOT NULL
          AND s.start_date >= p_from AND s.end_date <= p_to
        GROUP BY s.patient, s.start_date, s.end_date
        ORDER BY f DESC;
    v_stay    c_stays%ROWTYPE;
    v_patient patients%ROWTYPE;
BEGIN
    OPEN c_stays;
    FETCH c_stays INTO v_stay;               -- only the first row: the costliest
    CLOSE c_stays;
    IF v_stay.patient IS NULL THEN
        DBMS_OUTPUT.PUT_LINE('No finished stays in the period');
        RETURN;
    END IF;

    SELECT * INTO v_patient FROM patients WHERE tax_code = v_stay.patient;
    DBMS_OUTPUT.PUT_LINE('Patient: ' || v_patient.first_name || ' ' || v_patient.last_name
                         || ' (' || v_patient.tax_code || ')');
    DBMS_OUTPUT.PUT_LINE('Stay: ' || TO_CHAR(v_stay.start_date, 'YYYY-MM-DD') || ' to '
                         || TO_CHAR(v_stay.end_date, 'YYYY-MM-DD') || ', ' || v_stay.days
                         || ' days, F = ' || v_stay.f);
    FOR t IN (SELECT t.name, t.cost
              FROM stay_treatments st JOIN treatments t ON t.code = st.treatment
              WHERE st.patient = v_stay.patient AND st.stay_start = v_stay.start_date
              ORDER BY t.cost DESC) LOOP
        DBMS_OUTPUT.PUT_LINE('  ' || t.name || ': ' || t.cost);
    END LOOP;
END;
/

EXEC costliest_stay(DATE '2026-01-01', DATE '2026-06-30')

schema.sql

CREATE TABLE patients (
    tax_code   VARCHAR2(16) PRIMARY KEY,
    first_name VARCHAR2(40) NOT NULL,
    last_name  VARCHAR2(40) NOT NULL,
    sex        CHAR(1),
    birth_date DATE
);
CREATE TABLE treatments (code VARCHAR2(10) PRIMARY KEY, name VARCHAR2(60) NOT NULL, cost NUMBER NOT NULL);
CREATE TABLE stays (
    patient    VARCHAR2(16) REFERENCES patients,
    start_date DATE NOT NULL,
    end_date   DATE,                                 -- NULL: still in hospital
    PRIMARY KEY (patient, start_date)
);
CREATE TABLE stay_treatments (
    patient    VARCHAR2(16),
    stay_start DATE,
    treatment  VARCHAR2(10) REFERENCES treatments,
    FOREIGN KEY (patient, stay_start) REFERENCES stays
);

INSERT INTO patients VALUES ('RSSMRA80A01H501U', 'Mario', 'Rossi',   'M', DATE '1980-01-01');
INSERT INTO patients VALUES ('BNCLRA75B41F205X', 'Laura', 'Bianchi', 'F', DATE '1975-02-01');
INSERT INTO patients VALUES ('VRDGPP90C10A944K', 'Giuseppe', 'Verdi', 'M', DATE '1990-03-10');
INSERT INTO treatments VALUES ('XR',  'X-ray',       50);
INSERT INTO treatments VALUES ('MRI', 'MRI scan',   300);
INSERT INTO treatments VALUES ('SRG', 'Surgery',   1200);
INSERT INTO treatments VALUES ('BT',  'Blood test',  20);

-- Rossi: 3 days, 50 + 300 + 20 = 370      -> F = 20 + 370 - 3*2    = 384
INSERT INTO stays VALUES ('RSSMRA80A01H501U', DATE '2026-03-01', DATE '2026-03-04');
INSERT INTO stay_treatments VALUES ('RSSMRA80A01H501U', DATE '2026-03-01', 'XR');
INSERT INTO stay_treatments VALUES ('RSSMRA80A01H501U', DATE '2026-03-01', 'MRI');
INSERT INTO stay_treatments VALUES ('RSSMRA80A01H501U', DATE '2026-03-01', 'BT');
-- Bianchi: 10 days, 1200 + 20 + 20 = 1240 -> F = 15 + 1240 - 10*1.5 = 1240
INSERT INTO stays VALUES ('BNCLRA75B41F205X', DATE '2026-04-10', DATE '2026-04-20');
INSERT INTO stay_treatments VALUES ('BNCLRA75B41F205X', DATE '2026-04-10', 'SRG');
INSERT INTO stay_treatments VALUES ('BNCLRA75B41F205X', DATE '2026-04-10', 'BT');
INSERT INTO stay_treatments VALUES ('BNCLRA75B41F205X', DATE '2026-04-10', 'BT');
-- Verdi: still in hospital, more expensive, must be ignored
INSERT INTO stays VALUES ('VRDGPP90C10A944K', DATE '2026-04-15', NULL);
INSERT INTO stay_treatments VALUES ('VRDGPP90C10A944K', DATE '2026-04-15', 'SRG');
INSERT INTO stay_treatments VALUES ('VRDGPP90C10A944K', DATE '2026-04-15', 'SRG');
-- Rossi again, outside the period
INSERT INTO stays VALUES ('RSSMRA80A01H501U', DATE '2026-09-01', DATE '2026-09-02');
INSERT INTO stay_treatments VALUES ('RSSMRA80A01H501U', DATE '2026-09-01', 'SRG');
INSERT INTO stay_treatments VALUES ('RSSMRA80A01H501U', DATE '2026-09-01', 'SRG');
COMMIT;