course 5 module 4 assigment part 2 - amazon s3€¦ ·  · 2016-05-15course 5 module 4 assigment...

15
COURSE 5 MODULE 4 ASSIGMENT PART 2 Table 2: Analytic Query Requirements for Location Quantity Trends Analytic Query Analytic Functions Other Columns Notes AQ1: Cumulative quantity for locations Cumulative sum of amount ordered; Restart cumulative sum by location name and year; Use contract month as the ordering criteria; cumulative sum involves current row and all preceding rows Location name, contract year, contract month, sum of job amount ordered Extends location/sales class summary (BQ1) AQ2: Moving average of average amount ordered for locations Moving average of average amount ordered; Restart moving average by location name; Use combination of contract year and month as the ordering criteria; moving average of current row and 11 preceding rows Location name, contract year, contract month, average of job amount ordered Extends location/sales class summary (BQ1)

Upload: dangcong

Post on 30-Apr-2018

217 views

Category:

Documents


3 download

TRANSCRIPT

COURSE 5 MODULE 4 ASSIGMENT

PART 2

Table 2: Analytic Query Requirements for Location Quantity Trends

Analytic Query Analytic Functions Other Columns Notes AQ1: Cumulative quantity for locations

Cumulative sum of amount ordered; Restart cumulative sum by location name and year; Use contract month as the ordering criteria; cumulative sum involves current row and all preceding rows

Location name, contract year, contract month, sum of job amount ordered

Extends location/sales class summary (BQ1)

AQ2: Moving average of average amount ordered for locations

Moving average of average amount ordered; Restart moving average by location name; Use combination of contract year and month as the ordering criteria; moving average of current row and 11 preceding rows

Location name, contract year, contract month, average of job amount ordered

Extends location/sales class summary (BQ1)

Table 2: Analytic Query Requirements for Location Revenue Trends

Analytic Query Analytic Functions Other Columns Notes AQ3: Rank locations by descending sum of annual profit

Rank in descending order of annual sum of profit; Restart ranks for each year

Location name, contract year, contract month, sum of profit

Use views for BQ2 and BQ3 in the FROM clause; Sum of profit calculated as sum of invoice amount minus sum of total costs (labor, material, machine, and overhead)

AQ4: Rank locations by descending annual profit margin

Rank in descending order of annual profit margin; Restart ranks for each year

Location name, contract year, contract month, profit margin

Use views for BQ2 and BQ3 in the FROM clause; Profit margin calculated as annual sum of profit divided by annual sum of invoice amount; See AQ3 for calculation of sum of profit

AQ5: Percent rank of job profit margins for locations

Percent rank of profit margin for jobs; Use all jobs so no restarting of percent ranks

Job id, location name, contract year, contract month, profit margin

Use views for BQ2 and BQ3 in the FROM clause; See AQ4 for calculation of profit margin

AQ6: Top performers of percent rank of job profit margins for locations

Percent rank of profit margin for jobs; Use all jobs so no restarting of percent ranks

Job id, location name, contract year, contract month, profit margin

Refinement of AQ5 to show only top 5% of job profit margins; Use AQ5 in the FROM clause

Table 4: Analytic Query Requirements for Return Summaries

Analytic Query Analytic Functions Other Columns Notes AQ7: Rank sales class by return quantities for each year

Rank in descending order of sum of return quantity; Restart ranks for each year of the date sent year

Sales class description, year of date sent, sum of return quantity

Extends return summary (BQ4)

AQ8: Ratio to report of return quantities for sales classes by year

Ratio to report of sum of return quantity; Restart ratios for each date sent year

Sales class description, year of date sent, sum of return quantity

Extends return summary (BQ4); order by year and return quantity

Table 5: Analytic Query Requirements for Contractual Delay Summaries

Analytic Query Analytic Functions Other Columns Notes AQ9: Rank locations by sum of business days delayed for the job shipped by date (first shipment)

Rank in descending order of sum of business days delayed; Use both ranking functions; Restart rankings on year of shipped by date

Location name, year of date promised, sum of difference in business days

Use view for shipped date delays (BQ6); view should only contain delayed jobs

AQ10: Rank locations by delay rate for jobs delayed on the last shipment date

Rank in descending order of delay rate; Restart rankings on year of date promised

Location name, year of date promised, count of delayed jobs, sum of difference in business days

Use view for last promised date delays (BQ5); view should only contain delayed jobs

AQ1

SELECT LOC.LOCATION_NAME, T.TIME_YEAR YEAR_CONTRACT, T.TIME_MONTH MONTH_CONTRACT, --SUM(QUANTITY_ORDERED*UNIT_PRICE) SUM_AMOUNT, SUM(SUM(QUANTITY_ORDERED*UNIT_PRICE)) OVER (PARTITION BY LOC.LOCATION_NAME, T.TIME_YEAR ORDER BY T.TIME_MONTH ROWS UNBOUNDED PRECEDING) CUM_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_NAME, T.TIME_YEAR, T.TIME_MONTH ;

AQ2

SELECT LOC.LOCATION_NAME, T.TIME_YEAR YEAR_CONTRACT, T.TIME_MONTH MONTH_CONTRACT, AVG(SUM(QUANTITY_ORDERED*UNIT_PRICE)) OVER (PARTITION BY LOC.LOCATION_NAME ORDER BY T.TIME_YEAR, T.TIME_MONTH ROWS 11 PRECEDING ) AS CUM_11_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_NAME, T.TIME_YEAR, T.TIME_MONTH ;

AQ3

SELECT RANK() OVER (PARTITION BY YEAR_CONTRACT ORDER BY LOC_TOTAL_ANUAL_PROFIT DESC) RANKING, DAT.* FROM (SELECT A.LOCATION_NAME, A.YEAR_CONTRACT, A.MONTH_CONTRACT, SUM(SUM(A.SUM_INVOICE_AMOUNT-B.TOTAL_COSTS)) OVER (PARTITION BY A.LOCATION_NAME, A.YEAR_CONTRACT) LOC_TOTAL_ANUAL_PROFIT FROM IDWH_SPE_BQ2 A, IDWH_SPE_BQ3 B WHERE A.JOB_ID=B.JOB_ID GROUP BY A.LOCATION_NAME, A.YEAR_CONTRACT, A.MONTH_CONTRACT ORDER BY 4 DESC) DAT ORDER BY 1, YEAR_CONTRACT, MONTH_CONTRACT, LOC_TOTAL_ANUAL_PROFIT ;

AQ4

SELECT RANK() OVER (PARTITION BY YEAR_CONTRACT ORDER BY LOC_TOTAL_PROFIT_MARGIN DESC) RANKING, DAT.* FROM (SELECT A.LOCATION_NAME, A.YEAR_CONTRACT, A.MONTH_CONTRACT, SUM(SUM(A.SUM_INVOICE_AMOUNT-B.TOTAL_COSTS)) OVER (PARTITION BY A.LOCATION_NAME, A.YEAR_CONTRACT)/SUM(SUM(A.SUM_INVOICE_AMOUNT)) OVER (PARTITION BY A.LOCATION_NAME, A.YEAR_CONTRACT) LOC_TOTAL_PROFIT_MARGIN, --SUM(SUM(B.TOTAL_COSTS)) OVER (PARTITION BY A.LOCATION_NAME, A.YEAR_CONTRACT) LOC_TOTAL_COST_AMOUNT, SUM(SUM(A.SUM_INVOICE_AMOUNT-B.TOTAL_COSTS)) OVER (PARTITION BY A.LOCATION_NAME, A.YEAR_CONTRACT) LOC_TOTAL_ANUAL_PROFIT FROM IDWH_SPE_BQ2 A, IDWH_SPE_BQ3 B WHERE A.JOB_ID=B.JOB_ID GROUP BY A.LOCATION_NAME, A.YEAR_CONTRACT, A.MONTH_CONTRACT ORDER BY 4 DESC) DAT ORDER BY 1, YEAR_CONTRACT, MONTH_CONTRACT, LOC_TOTAL_ANUAL_PROFIT ;

AQ5

SELECT PERCENT_RANK() OVER (ORDER BY JOB_TOTAL_PROFIT_MARGIN DESC) RANKING, DAT.* FROM (SELECT A.JOB_ID, A.LOCATION_NAME, A.YEAR_CONTRACT, A.MONTH_CONTRACT, SUM(SUM(A.SUM_INVOICE_AMOUNT-B.TOTAL_COSTS)) OVER (PARTITION BY A.JOB_ID)/SUM(SUM(A.SUM_INVOICE_AMOUNT)) OVER (PARTITION BY A.JOB_ID) JOB_TOTAL_PROFIT_MARGIN, SUM(SUM(A.SUM_INVOICE_AMOUNT-B.TOTAL_COSTS)) OVER (PARTITION BY A.JOB_ID) JOB_TOTAL_ANUAL_PROFIT FROM IDWH_SPE_BQ2 A, IDWH_SPE_BQ3 B WHERE A.JOB_ID=B.JOB_ID GROUP BY A.JOB_ID, A.LOCATION_NAME, A.YEAR_CONTRACT, A.MONTH_CONTRACT ORDER BY 4 DESC) DAT ORDER BY 1, YEAR_CONTRACT, MONTH_CONTRACT, JOB_TOTAL_ANUAL_PROFIT ;

OR AGGREGATED BY LOCATION:

SELECT PERCENT_RANK() OVER (ORDER BY LOC_TOTAL_PROFIT_MARGIN DESC) RANKING, DAT.* FROM (SELECT A.JOB_ID, A.LOCATION_NAME, A.YEAR_CONTRACT, A.MONTH_CONTRACT, SUM(SUM(A.SUM_INVOICE_AMOUNT-B.TOTAL_COSTS)) OVER (PARTITION BY A.LOCATION_NAME)/SUM(SUM(A.SUM_INVOICE_AMOUNT)) OVER (PARTITION BY A.LOCATION_NAME) LOC_TOTAL_PROFIT_MARGIN, SUM(SUM(A.SUM_INVOICE_AMOUNT-B.TOTAL_COSTS)) OVER (PARTITION BY A.LOCATION_NAME) LOC_TOTAL_ANUAL_PROFIT FROM IDWH_SPE_BQ2 A, IDWH_SPE_BQ3 B WHERE A.JOB_ID=B.JOB_ID GROUP BY A.JOB_ID, A.LOCATION_NAME, A.YEAR_CONTRACT, A.MONTH_CONTRACT ORDER BY 3 DESC) DAT ORDER BY 1, YEAR_CONTRACT, MONTH_CONTRACT, LOC_TOTAL_ANUAL_PROFIT ;

AQ6

SELECT * FROM (SELECT PERCENT_RANK() OVER (ORDER BY JOB_TOTAL_PROFIT_MARGIN DESC) D_RANKING, DAT.* FROM (SELECT A.JOB_ID, A.LOCATION_NAME, A.YEAR_CONTRACT, A.MONTH_CONTRACT, SUM(SUM(A.SUM_INVOICE_AMOUNT-B.TOTAL_COSTS)) OVER (PARTITION BY A.JOB_ID)/SUM(SUM(A.SUM_INVOICE_AMOUNT)) OVER (PARTITION BY A.JOB_ID) JOB_TOTAL_PROFIT_MARGIN, SUM(SUM(A.SUM_INVOICE_AMOUNT-B.TOTAL_COSTS)) OVER (PARTITION BY A.JOB_ID) JOB_TOTAL_ANUAL_PROFIT FROM IDWH_SPE_BQ2 A, IDWH_SPE_BQ3 B WHERE A.JOB_ID=B.JOB_ID GROUP BY A.JOB_ID, A.LOCATION_NAME, A.YEAR_CONTRACT, A.MONTH_CONTRACT ORDER BY 4 DESC) DAT ORDER BY 1, YEAR_CONTRACT, MONTH_CONTRACT, JOB_TOTAL_ANUAL_PROFIT ) WHERE D_RANKING <=0.05;

OR AGGREGATED BY LOCATION

SELECT * FROM (SELECT PERCENT_RANK() OVER (ORDER BY LOC_TOTAL_PROFIT_MARGIN DESC) D_RANKING, DAT.* FROM (SELECT A.JOB_ID, A.LOCATION_NAME, A.YEAR_CONTRACT, A.MONTH_CONTRACT, SUM(SUM(A.SUM_INVOICE_AMOUNT-B.TOTAL_COSTS)) OVER (PARTITION BY A.LOCATION_NAME)/SUM(SUM(A.SUM_INVOICE_AMOUNT)) OVER (PARTITION BY A.LOCATION_NAME) LOC_TOTAL_PROFIT_MARGIN, SUM(SUM(A.SUM_INVOICE_AMOUNT-B.TOTAL_COSTS)) OVER (PARTITION BY A.LOCATION_NAME) LOC_TOTAL_ANUAL_PROFIT FROM IDWH_SPE_BQ2 A, IDWH_SPE_BQ3 B WHERE A.JOB_ID=B.JOB_ID GROUP BY A.JOB_ID, A.LOCATION_NAME, A.YEAR_CONTRACT, A.MONTH_CONTRACT ORDER BY 4 DESC) DAT ORDER BY 1, YEAR_CONTRACT, MONTH_CONTRACT, LOC_TOTAL_ANUAL_PROFIT ) WHERE D_RANKING <=0.05;

AQ7

SELECT SALES_CLASS_DESC, YEAR_INVOICE_SENT, SUM(SUM_QTY_RETURN) SUM_RETURN_QTY, RANK() OVER (PARTITION BY YEAR_INVOICE_SENT ORDER BY SUM(SUM_QTY_RETURN) DESC) AS RANK_RETURN_QTY FROM IDWH_SPE_BQ4 GROUP BY SALES_CLASS_DESC, YEAR_INVOICE_SENT;

AQ8

SELECT SALES_CLASS_DESC, YEAR_INVOICE_SENT, SUM(SUM_QTY_RETURN) SUM_RETURN_QTY, RATIO_TO_REPORT(SUM(SUM_QTY_RETURN)) OVER (PARTITION BY YEAR_INVOICE_SENT) AS SUM_CLASS_RATIO --,SUM(SUM(SUM_QTY_RETURN)) OVER (PARTITION BY YEAR_INVOICE_SENT) SUM_YEAR_RETURNS -- TO TEST FROM IDWH_SPE_BQ4 GROUP BY SALES_CLASS_DESC, YEAR_INVOICE_SENT ORDER BY YEAR_INVOICE_SENT, SUM(SUM_QTY_RETURN) DESC;

AQ9 SELECT LOCATION_NAME, T.TIME_YEAR DATE_SHIP_BY_YEAR, SUM(SH_CD_DIFF_DAYS) SUM_DIFF_DAYS, RANK() OVER (PARTITION BY T.TIME_YEAR ORDER BY SUM(SH_CD_DIFF_DAYS) DESC) AS RANK_DIFF_DAYS, DENSE_RANK() OVER (PARTITION BY T.TIME_YEAR ORDER BY SUM(SH_CD_DIFF_DAYS) DESC) AS D_RANK_DIFF_DAYS FROM IDWH_SPE_BQ6 BQ6, W_TIME_D T WHERE BQ6.DATE_SHIP_BY=T.TIME_ID GROUP BY LOCATION_NAME, T.TIME_YEAR;

AQ10

SELECT LOCATION_NAME, T.TIME_YEAR DATE_PROMISED_YEAR, COUNT(JOB_ID) DELAYED_JOBS, RANK() OVER (PARTITION BY T.TIME_YEAR ORDER BY COUNT(JOB_ID) DESC) AS RANK_DELAYED_JOBS, DENSE_RANK() OVER (PARTITION BY T.TIME_YEAR ORDER BY COUNT(JOB_ID) DESC) AS D_RANK_DELAYED_JOBS FROM IDWH_SPE_BQ5 BQ5, W_TIME_D T WHERE BQ5.DATE_PROMISED_ID=T.TIME_ID GROUP BY LOCATION_NAME, T.TIME_YEAR;