course 5 module 4 assigment part 1 - amazon s3€¦ · · 2016-05-15course 5 module 4 assigment...
TRANSCRIPT
COURSE 5 MODULE 4 ASSIGMENT
PART 1
Table 1: Base Query (BQ) Requirements Query (Area) Result Columns Comments BQ1: Location/Sales class summary for job quantity and amount (revenue/costs)
Location id, location name, sales class id, sales class description, year of job contract date, month of job contract date, base price of sales class, sum of quantity ordered, sum of job amount
Job amount defined as quantity ordered times unit price; combine with time dimension table on contract date FK
BQ2: Location invoice revenue summary (revenue/costs)
Job id, location id, location name, job unit price, job order quantity, year of contract date, month of contract date, sum of invoice amount, sum of invoice quantity
Need to combine some fact tables; Contract year/month used to match revenues and costs; Define a CREATE VIEW statement in addition to the SELECT statement.
BQ3: Location subjob cost summary (revenue/costs)
Job id, location id, location name, year of contract date, month of contract date, sum of labor cost, sum of material cost, sum of machine cost, sum of overhead costs, sum of total costs, sum of quantity produced, unit cost
Need to combine some fact tables; machine costs computed as machine hours times rate per hour for machine; create a view; Contract year/month used to match revenues and costs; Unit cost defined as total costs / sum of quantity produced
BQ 4: Returns by location and sales class (quality control)
Location id, location name, sales class id, sales class description, year of invoice sent date, month of invoice sent date sum of quantity returned, sum of dollar amount of returns
Return quantity defined as quantity shipped minus quantity invoiced; condition for positive return quantity
BQ5: Last shipment delays involving date promised (quality control)
Job id, location id, location name, sales class id, sales class description, time id of the date promised for the job, time id of the last shipment date, order quantity in the job, sum of shipped quantity after job promised date, difference in business days between last shipment date and promised date
Condition for last shipment date later than job promised date; use function provided for computing difference in business days; requires a nested query in the FROM clause to determine first shipment date for a job; Define a CREATE VIEW statement in addition to the SELECT statement; See SELECT statement on page 4
BQ 6: First shipment delays involving shipped by date (quality control)
Job id, Location id, location name, sales class id, sales class description, time id of the shipped by date of the job, time id of the first shipment date, difference in business days between first shipment date and contractual shipped by date
Condition for first shipment date later than job shipped by date; use function provided for computing difference in business days; a nested query in the FROM clause to determine first shipment date for a job; Define a CREATE VIEW statement in addition to the SELECT statement; See SELECT statement on page 4
BQ1
SELECT LOC.LOCATION_ID, LOC.LOCATION_NAME, SC.SALES_CLASS_ID, SC.SALES_CLASS_DESC, T.TIME_YEAR YEAR_CONTRACT, T.TIME_MONTH MONTH_CONTRACT, SC.BASE_PRICE, SUM(QUANTITY_ORDERED) SUM_QTY_ORDERED, SUM(QUANTITY_ORDERED*UNIT_PRICE) SUM_AMOUNT FROM W_LOCATION_D LOC, W_JOB_F J, W_SALES_CLASS_D SC, W_TIME_D T WHERE J.LOCATION_ID=LOC.LOCATION_ID AND J.SALES_CLASS_ID=SC.SALES_CLASS_ID AND T.TIME_ID=J.CONTRACT_DATE GROUP BY LOC.LOCATION_ID, LOC.LOCATION_NAME, SC.SALES_CLASS_ID, SC.SALES_CLASS_DESC, T.TIME_YEAR, T.TIME_MONTH , SC.BASE_PRICE;
BQ2
CREATE OR REPLACE VIEW IDWH_SPE_BQ2 AS SELECT J.JOB_ID, LOC.LOCATION_ID, LOC.LOCATION_NAME, J.UNIT_PRICE, J.QUANTITY_ORDERED, T.TIME_YEAR YEAR_CONTRACT, T.TIME_MONTH MONTH_CONTRACT, SUM(INV.INVOICE_AMOUNT) SUM_INVOICE_AMOUNT, SUM(INV.INVOICE_QUANTITY) SUM_INVOICE_QUANTITY FROM W_LOCATION_D LOC, W_JOB_F J, W_INVOICELINE_F INV, W_TIME_D T WHERE J.LOCATION_ID=LOC.LOCATION_ID AND J.SALES_CLASS_ID=INV.SALES_CLASS_ID AND J.SALES_AGENT_ID=INV.SALES_AGENT_ID AND J.LOCATION_ID=INV.LOCATION_ID AND J.CUST_ID_ORDERED_BY=INV.CUST_KEY AND T.TIME_ID=J.CONTRACT_DATE GROUP BY J.JOB_ID, LOC.LOCATION_ID, LOC.LOCATION_NAME, J.UNIT_PRICE, J.QUANTITY_ORDERED, T.TIME_YEAR , T.TIME_MONTH;
BQ3
CREATE OR REPLACE VIEW IDWH_SPE_BQ3 AS SELECT J.JOB_ID, LOC.LOCATION_ID, LOC.LOCATION_NAME, T.TIME_YEAR YEAR_CONTRACT, T.TIME_MONTH MONTH_CONTRACT, SUM(COST_LABOR) SUM_COST_LABOUR, SUM(COST_MATERIAL) SUM_COST_MATERIAL, SUM(SUB.MACHINE_HOURS*MAC.RATE_PER_HOUR) SUM_MACHINE_COST, SUM(COST_OVERHEAD) SUM_OVERHEAD_COST, SUM(COST_LABOR+COST_MATERIAL+(SUB.MACHINE_HOURS*MAC.RATE_PER_HOUR)+COST_OVERHEAD) TOTAL_COSTS, SUM(QUANTITY_PRODUCED) TOTAL_QTY_PRODUCED, SUM(COST_LABOR+COST_MATERIAL+(SUB.MACHINE_HOURS*MAC.RATE_PER_HOUR)+COST_OVERHEAD) / SUM(QUANTITY_PRODUCED) UNIT_COST FROM W_LOCATION_D LOC, W_JOB_F J, W_SUB_JOB_F SUB, W_MACHINE_TYPE_D MAC, W_TIME_D T WHERE J.LOCATION_ID=LOC.LOCATION_ID AND J.JOB_ID=SUB.JOB_ID AND SUB.MACHINE_TYPE_ID=MAC.MACHINE_TYPE_ID AND T.TIME_ID=J.CONTRACT_DATE GROUP BY J.JOB_ID, LOC.LOCATION_ID, LOC.LOCATION_NAME, T.TIME_YEAR , T.TIME_MONTH ;
BQ4
CREATE OR REPLACE VIEW IDWH_SPE_BQ4 AS SELECT LOC.LOCATION_ID, LOC.LOCATION_NAME, SC.SALES_CLASS_ID, SC.SALES_CLASS_DESC, T.TIME_YEAR YEAR_INVOICE_SENT, T.TIME_MONTH MONTH_INVOICE_SENT, SUM(INV.QUANTITY_SHIPPED-INV.INVOICE_QUANTITY) SUM_QTY_RETURN, SUM(SH.SHIPPED_AMOUNT-INV.INVOICE_AMOUNT) SUM_DOLLAR_RETURN FROM W_LOCATION_D LOC, W_JOB_SHIPMENT_F SH, W_SALES_CLASS_D SC, W_INVOICELINE_F INV, W_TIME_D T WHERE SH.INVOICE_ID=INV.INVOICE_ID AND SH.LOCATION_ID=LOC.LOCATION_ID AND INV.LOCATION_ID=LOC.LOCATION_ID AND SC.SALES_CLASS_ID=INV.SALES_CLASS_ID AND SC.SALES_CLASS_ID=SH.SALES_CLASS_ID AND T.TIME_ID=INV.INVOICE_SENT_DATE GROUP BY LOC.LOCATION_ID, LOC.LOCATION_NAME, SC.SALES_CLASS_ID, SC.SALES_CLASS_DESC, T.TIME_YEAR , T.TIME_MONTH HAVING SUM(INV.QUANTITY_SHIPPED-INV.INVOICE_QUANTITY) > 0 order by 1,4,5,6 ;
BQ5
CREATE OR REPLACE VIEW IDWH_SPE_BQ5 AS SELECT J.JOB_ID, LOC.LOCATION_ID, LOC.LOCATION_NAME, SC.SALES_CLASS_ID, SC.SALES_CLASS_DESC, T1.TIME_ID DATE_PROMISED_ID, T2.TIME_ID LAST_SHIP_DATE_ID, J.QUANTITY_ORDERED, X1.SumDelayShipQty, getBusDaysDiff(T2.TIME_ID,T1.TIME_ID) SH_DP_DIFF_DAYS FROM W_JOB_F J, W_LOCATION_D LOC, W_SALES_CLASS_D SC, W_TIME_D T1, W_TIME_D T2, (SELECT W_SUB_JOB_F.JOB_ID, MAX(actual_ship_Date) AS Last_Shipment_Date, SUM ( actual_Quantity ) AS SumDelayShipQty FROM W_JOB_SHIPMENT_F, W_SUB_JOB_F, W_Job_F WHERE W_SUB_JOB_F.SUB_JOB_ID = W_JOB_SHIPMENT_F.SUB_JOB_ID AND W_Job_F.Job_Id = W_SUB_JOB_F.JOB_ID AND Actual_Ship_Date > Date_Promised GROUP BY W_SUB_JOB_F.JOB_ID ) X1 WHERE J.JOB_ID=X1.JOB_ID AND J.LOCATION_ID=LOC.LOCATION_ID AND SC.SALES_CLASS_ID=J.SALES_CLASS_ID AND T1.TIME_ID=J.DATE_PROMISED AND T2.TIME_ID=X1.Last_Shipment_Date --AND T2.TIME_ID > T1.TIME_ID NOT NECESSARY IT COMES FILTERED FROM X1 order by JOB_ID,DATE_PROMISED_ID,SH_DP_DIFF_DAYS;
BQ6 CREATE OR REPLACE VIEW IDWH_SPE_BQ6 AS SELECT J.JOB_ID, LOC.LOCATION_ID, LOC.LOCATION_NAME, SC.SALES_CLASS_ID, SC.SALES_CLASS_DESC, T1.TIME_ID DATE_SHIP_BY, T2.TIME_ID FIRST_SHIP_DATE_ID, getBusDaysDiff(T2.TIME_ID,T1.TIME_ID) SH_CD_DIFF_DAYS FROM W_JOB_F J, W_LOCATION_D LOC, W_SALES_CLASS_D SC, W_TIME_D T1, W_TIME_D T2, ( SELECT W_SUB_JOB_F.JOB_ID, MIN(Actual_Ship_Date) as FirstShipDate FROM W_JOB_SHIPMENT_F, W_SUB_JOB_F WHERE W_SUB_JOB_F.SUB_JOB_ID = W_JOB_SHIPMENT_F.SUB_JOB_ID GROUP BY W_SUB_JOB_F.JOB_ID ) X1 WHERE J.JOB_ID=X1.JOB_ID AND J.LOCATION_ID=LOC.LOCATION_ID AND SC.SALES_CLASS_ID=J.SALES_CLASS_ID AND T1.TIME_ID=J.DATE_SHIP_BY AND T2.TIME_ID=X1.FirstShipDate AND T2.TIME_ID > T1.TIME_ID order by JOB_ID,DATE_SHIP_BY,SH_CD_DIFF_DAYS;