1. describe RCDW_cHARTER_fACT;
describe RCDW_Aircraft_Dim;
Select d.AIRCRAFT_ID,
d.MANUFACTURER,
d.MODEL_NAME,
f.Hours_Flown
from RCDW_Charter_Fact f
join RCDW_Aircraft_Dim d
using (Aircraft_Key)
group by Aircraft_Id;
order by d.Aircraft_Id;
--2.1 A. Using the charter flights database, create an SQL query to return the
aircraft ID, manufacturer, model name and total hours flown (on charter flights)
for each of company's aircraft that has flown any charter flights.
Select AIRCRAFT_ID,
MANUFACTURER,
MODEL_NAME,
Sum(Hours_Flown) as "Total Hours Flown"
from RCDW_Charter_Fact
join RCDW_Aircraft_Dim
using (Aircraft_Key)
group by Aircraft_ID, MANUFACTURER,
MODEL_NAME
order by Manufacturer, Model_Name, Aircraft_Id;
--2.1 B. Modify the above query to only return total hours flown for charter
flights
during February of 2013.
Select AIRCRAFT_ID,
MANUFACTURER,
MODEL_NAME,
Sum(Hours_Flown) as "Total Hours Flown"
from RCDW_Charter_Fact
join RCDW_Aircraft_Dim
using (Aircraft_Key)
join RCDW_Date_Dim
using (Date_Key)
where Month_Num = 02
and Year = 2013
group by Aircraft_ID, MANUFACTURER,
MODEL_NAME
order by Manufacturer, Model_Name, Aircraft_Id;
--2.1 C. Modify the above query to only return total hours flown for charter
flights that took place
ON WEEKENDS during February of 2013.
Select * from RCDW_Date_Dim;
Select AIRCRAFT_ID,
MANUFACTURER,
MODEL_NAME,
Sum(Hours_Flown) as "Total Hours Flown"
from RCDW_Charter_Fact
join RCDW_Aircraft_Dim
using (Aircraft_Key)
join RCDW_Date_Dim
using (Date_Key)
where Weekend_Flag = 'Y'
and Month_Num = 02
and Year = 2013
group by Aircraft_ID, MANUFACTURER,
MODEL_NAME
2. order by Manufacturer, Model_Name, Aircraft_Id;
--2.2 A. Using the charter flights database, create an SQL query to return the
customer ID, first name,
last name, phone number, destination, trip date, & amount billled for each
charter flight in the company's data warehouse.
Select * from RCDW_Date_Dim;
Select Trip_Id,
Trip_Date,
Customer_ID,
First_Name,
Last_Name,
Phone_Num,
Destination,
Amount_Billed
-- Sum(Hours_Flown) as "Total Hours Flown"
from RCDW_Customer_Dim
join RCDW_Charter_Fact
using (Customer_Key)
join RCDW_Trip_Dim
using (Trip_Key)
order by Trip_Date, Trip_Id;
select * from RCDW_Charter_Fact;
--2.2 B. Modify the above query to also return the total distance flown, and
R.C. Charter's
estimated profit for the charter. (calculate estimated profit as amount billed
minus trip cost)
Select * from RCDW_Date_Dim;
Select Trip_Id,
Trip_Date,
Customer_ID,
First_Name,
Last_Name,
Phone_Num,
Destination,
Amount_Billed,
Distance,
(Amount_Billed - Trip_Cost) as "Estimated Profit"
from RCDW_Customer_Dim
join RCDW_Charter_Fact
using (Customer_Key)
join RCDW_Trip_Dim
using (Trip_Key)
order by Trip_Date, Trip_Id;
--2.2. C. Modify the above query to only return the selected columns for
charter flights
that ocurred on Mondays durng the year 2013.
Select Trip_Id,
Trip_Date,
Day_Of_Week,
Customer_ID,
First_Name,
Last_Name,
Phone_Num,
Destination,
Amount_Billed,
Distance,
(Amount_Billed - Trip_Cost) as "Estimated Profit"
from RCDW_Customer_Dim
join RCDW_Charter_Fact
using (Customer_Key)
join RCDW_Trip_Dim
3. using (Trip_Key)
join RCDW_Date_Dim
using (Date_Key)
where Day_Of_Week = 'Monday'
and year = 2013
order by Trip_Date, Trip_Id;
--3.1.A Write an Oracle stored function that will accept as input a pilot's
employee ID, a begin date
and an end date, and return the total number of hours that pilot as flown in the
time period repre-
sented by the dates from your data warehouse. The total should include both
hours flown as
the pilot and those flown as the copilot. Include any charter flights for which
the trip date in in the date range.
select * from RCDW_Date_Dim;
select * from RCDW_Charter_Fact;
select sum(Hours_Flown)
from RCDW_Charter_Fact ch,
RCDW_Date_Dim d,
RCDW_Pilot_Dim p
-- top half get hours flown as Pilot
where ( (p.Pilot_Key = ch.Pilot_Key
and ch.Date_Key = d.Date_Key)
and d.day between '01-Jan-13' and '01-Mar-14'
and d.Year = 2013
and p.Employee_Id = 109 )
or
-- bottom half get hours flown as Copilot
( (p.Pilot_Key = ch.CoPilot_Key
and ch.Date_Key = d.Date_Key)
and d.day between '01-Jan-13' and '01-Mar-14'
and d.Year = 2013
and p.Employee_Id = 109 );
CREATE OR REPLACE FUNCTION Sf_Hours_Flown
(p_Employee_Id IN RCDW_Pilot_Dim.Employee_Id%TYPE
,p_Begin_Day IN RCDW_Date_Dim.Day%TYPE
,p_End_Day IN RCDW_Date_Dim.Day%TYPE
)
RETURN number
IS
v_Hours_Flown RCDW_Charter_Fact.Hours_Flown%TYPE := 0;
BEGIN
select sum(Hours_Flown) INTO v_Hours_Flown
from RCDW_Charter_Fact ch,
RCDW_Date_Dim d,
RCDW_Pilot_Dim p
-- top half get hours flown as Pilot
where ( (p.Pilot_Key = ch.Pilot_Key
and ch.Date_Key = d.Date_Key)
and d.day between p_Begin_Day and p_End_Day
and p.Employee_Id = p_Employee_Id )
or
-- bottom half get hours flown as Copilot
( (p.Pilot_Key = ch.CoPilot_Key
and ch.Date_Key = d.Date_Key)
and d.day between p_Begin_Day and p_End_Day
and p.Employee_Id = p_Employee_Id );
RETURN
v_Hours_Flown;
4. end Sf_Hours_Flown;
/
Show errors;
--3.1.B Demonstrate that your function works by using an SQL statement such as
the following
(assumes a function named sf_hours_flown):
SELECT Sf_Hours_Flown(109,'01-Jan-13','01-Dec-13') from Dual;
--3.2.A Write an Oracle stored procedure that accepts as input an aircraft ID,
a begin date and an end
date, and returns (as an Out parmeter) the total charge for that aircraft for
the date range. The total charge
should be calculated as distance times charge per mile for any charter flights
during the date range.
select * from RCDW_AIRCRAFT_DIM;
select SUM(Amount_Billed) from RCDW_CHARTER_FACT where aircraft_Key = 1000012;
CREATE OR REPLACE PROCEDURE PRC_Total_Charge
(p_Aircraft_Id IN RCDW_Aircraft_Dim.Aircraft_Id%TYPE ,
p_Begin_Day IN RCDW_Date_Dim.Day%TYPE ,
p_End_Day IN RCDW_Date_Dim.Day%TYPE ,
p_Total_Charge OUT RCDW_Charter_Fact.Amount_Billed%TYPE
)
IS
v_Distance RCDW_Charter_Fact.Distance%TYPE :=0;
v_Charge_per_Mile RCDW_Aircraft_Dim.Charge_per_Mile%TYPE :=0;
BEGIN
select SUM(Distance), SUM(Charge_Per_Mile), SUM(Distance) * SUM(Charge_Per_Mile)
as "Total Charges"
into v_Distance , v_Charge_per_Mile , p_Total_Charge
from RCDW_CHARTER_FACT
join RCDW_Date_Dim using (Date_Key)
join RCDW_AIRCRAFT_DIM using (Aircraft_Key)
where RCDW_Date_Dim.Day between p_Begin_Day and p_End_Day
and RCDW_AIRCRAFT_DIM.Aircraft_Id = p_Aircraft_Id;
DBMS_OUTPUT.PUT_LINE('Total charges for aircraft ' || p_Aircraft_Id || ' are
$' || p_Total_Charge);
end PRC_Total_Charge;
/
Show errors;
--3.2.b
SET SERVEROUTPUT ON;
declare
g_Total_Charge number;
BEGIN
PRC_Total_Charge('2289L','01-Jan-13','31-Dec-13', :g_Total_Charge);
DBMS_OUTPUT.PUT_LINE('Total charges for aircraft ' || '2289L ' || 'are $' ||
g_Total_Charge);
END;
/