Вы находитесь на странице: 1из 2

/* Run query to return table values and derived columns created using ANSI Windo w Function columns */ SELECT

ORDER_NO ,LINE_NO ,PRODUCT_ID ,PRODUCT_QT ,UNIT_RESALE_AM ,(UNIT_RESALE_AM * PRODUCT_QT) as [Extended Sales] /* vvvvv Begin derived columns using ANSI Window functions vvvvvv */ ,round( /* Rounding functi on to limit the number of digits */ (UNIT_RESALE_AM * PRODUCT_QT) /* Numerator: Exte nded Sales for the Order Line*/ / /* Divided by */ SUM(UNIT_RESALE_AM * PRODUCT_QT) OVER (PARTITION BY ORDER_NO) /* Denominator: Su m of extended sales by Order */ ,3) /* End of rounding function, limit the answer to three digits to the right of the decimal*/ as [Pcnt Sales For This Order] /* Give the calcul ation an alias */ ,round( /* Rounding fun ction to limit the number of digits */ (UNIT_RESALE_AM * PRODUCT_QT) /* Numerator: E xtended Sales for the Order Line*/ / /* Divided by * / SUM(UNIT_RESALE_AM * PRODUCT_QT) OVER (PARTITION BY PRODUCT_ID) /* Denominator: Sum of extended sales by Product for ALL Orders */ ,3) /* End of round ing function, limit the answer to three digits to the right of the decimal*/ as [Pcnt Sales For This Product] /* Give the cal culation an alias */

,round( /* Rounding function to limit the number of digits */ UNIT_RESALE_AM /* Numerator: Unit Price for the order line*/ / /* Divided by */ AVG(UNIT_RESALE_AM) OVER (PARTITION BY PRODUCT_ID) /* Denominator: Simple Averag e Unit Price for the Product for ALL orders. */ ,3) /* End of rounding function, limit the answer to three digits to the right of the decimal*/ as [Unit Am Pcnt of Simple Avg Unit Am] /* Give the calculation an al ias */

,round( ng function to limit the number of digits */ UNIT_RESALE_AM tor: Unit Price for the order line*/ /

/* Roundi /* Numera /* Divide

d by */ --vvvv Begin Definition of Denominator: Weighted Avg Unit Price vvvvvvvvvvvvvvvv vvvvvvvvvvvvvvvvvv (SUM(UNIT_RESALE_AM * PRODUCT_QT) OVER (PARTITION BY PRODUCT_ID) /* Sum of Unit Resale price by Product for ALL orders weighted by Unit (not dollar) Volum e */ / /* Divide d by */ SUM(PRODUCT_QT) OVER (PARTITION BY PRODUCT_ID)) /* Sum of Volume by Product for ALL orders */ --^^^^ End Definition of Denominator: Weighted Avg Unit Price ^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^ ,3) /* End of rounding function, limit the answer to three digits to the right of the decimal */ / * End of rounding function, limit the answer to three digits to the right of the decimal*/ as [Unit Am Pcnt of Wt Avg Unit Am] /* Give th e calculation an alias */

/* Note: embedded ORDER BY clause in expression below does not have to be the same as main ORDER BY clause. Change the embedded clause to get interesting results. For example, if your chan ged to ORDER BY PRODUCT_ID, then the Asc Gapless Line No would be based on product_id, and not merely a "squ eezing out" of the gaps in the database supplied LINE_NO. */ ,ROW_NUMBER() OVER (PARTITION BY ORDER_NO ORDER BY LINE_NO) as [Asc Gapless Line No] /* Only difference between the column directly below this comment and the column directly above this comment is the term 'desc' in the embedded ORDER BY c lause */ ,ROW_NUMBER() OVER (PARTITION BY ORDER_NO ORDER BY LINE_NO desc) as [Desc Gaples s Line No] /* ^^^^ End derived columns using ANSI Window functions ^^^^ */ FROM TEST_ANSI_WINDOW ORDER BY ORDER_NO, LINE_NO;

Вам также может понравиться