Real Cases of Indispensability    of Analytic Functions         Kim Berg Hansen      T. Hansen Gruppen A/S                ...
Who is Kim Berg Hansen?            ●   a Danish SQL and                  PL/SQL Developer:                    http://dspsd...
What will Kim talk about?   What is so great about analytic functions?   How are they used?   Case/Demo: Top selling it...
So, what is so great about analytics?   Normal SQL functions operate on one row   Aggregates can do more rows but loose ...
Access data from other rowsDEPTNO   ENAME    SAL     ●   LAG()------   ------ -----     ●   LEAD()    30   JAMES    950 1 ...
Choose which rowsDEPTNO ENAME    SAL            ●   SUM() OVER ()------- ------ -----     10 MILLER 1300     8750   ●   SU...
Syntax diagram analytic_function::= analytic_clause::=                        examples.sql                        #Kscope
Case/Demo: Top selling items   Classic task for a programmer:   Show top three by product group   Also how big percenta...
Case/Demo: Top selling items   What kind of top three do you wish?    ● DENSE_RANK()    ● RANK()    ● ROW_NUMBER()   PAR...
Case/Demo: Picking by FIFO   Items stored in different locations in warehouse   Pick an order by First-In First-Out prin...
Case/Demo: Picking by FIFO   SUM() by item   Ordered by purchase date   Rolling sum to find how much was picked by    ”...
Case/Demo: Efficient picking route   OK, we know what to pick   But is this a smart route to drive?                     ...
Case/Demo: Efficient picking route   Wouldn’t this be a smarter route?   We need to change direction every other aisle  ...
Case/Demo: Efficient picking route   What if we lack a door at each end?   Direction has to ”restart” per warehouse     ...
Case/Demo: Efficient picking route   DENSE_RANK() to number the aisles in order    visited   Order the output    ● ”Up” ...
Case/Demo: Picking efficiency   How fast can operators pick items?   How much do they wait idle for totes to arrive?    ...
Case/Demo: Picking efficiency   Log over tote missions arriving and departing    the picking stations   LEAD() on missio...
Case/Demo: Forecasting sales   Forecast the sales of next year   But follow the trend of the item                       ...
Case/Demo: Forecasting sales   REGR_SLOPE() to calculate trend   RANGE window for sliding trend calculation   ”Transpos...
Case/Demo: Forecast zero stock   Fireworks sell like crazy last week of December   What hour will a store run out of sto...
Case/Demo: Forecast zero stock   SUM() on budget sales data from ”now” forward   Identify hour when rolling sum exceeds ...
The never ending case list   We use analytic functions all the time       ●   WheelGuide®       ●   Replenish shop stock ...
You will find your own cases   Just start using analytics   The more you do the more often you find cases   When you st...
Any questions?                              ●   Download from Kscope:                                  ●   This presentati...
Upcoming SlideShare
Loading in …5
×

Real cases of indispensability of Oracle SQL analytic functions

1,823 views

Published on

Presentation on Oracle SQL analytic functions as presented by me at ODTUG KScope 2012

1 Comment
2 Likes
Statistics
Notes
  • Excellent presentation and very nice to find a fellow Oracle/Analytic SQL practitioner (who also likes SciFi!).

    I myself have been using Analytic SQLs heavily since 2007 to handle complex batch jobs on an Oracle VLDB and can testify to the extreme difference they make.

    Best wishes,

    Atish
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
1,823
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
63
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide
  • This is your opening slide.
  • Real cases of indispensability of Oracle SQL analytic functions

    1. 1. Real Cases of Indispensability of Analytic Functions Kim Berg Hansen T. Hansen Gruppen A/S #Kscope
    2. 2. Who is Kim Berg Hansen? ● a Danish SQL and PL/SQL Developer: http://dspsd.blogspot.com ● Professional geek since 1996 ● Oracle programmer since 2000 ● Single SQL Statement mantra (yes, fan of Thomas Kyte ) ● Likes to cook ● Reads sci-fi #Kscope
    3. 3. What will Kim talk about? What is so great about analytic functions? How are they used? Case/Demo: Top selling items Case/Demo: Picking by FIFO Case/Demo: Efficient picking route Case/Demo: Picking efficiency Case/Demo: Forecasting sales Case/Demo: Forecast zero firework stock Any questions? #Kscope
    4. 4. So, what is so great about analytics? Normal SQL functions operate on one row Aggregates can do more rows but loose detail When you need details together with subtotals, ranks, ratios, comparisons, you could do: Client operations (tool or code with variables/arrays) Scalar subqueries (multiple access of same data) Analytic functions (often much more efficient ) Analytics allow you to operate across the entire resultset, not just a single row #Kscope
    5. 5. Access data from other rowsDEPTNO ENAME SAL ● LAG()------ ------ ----- ● LEAD() 30 JAMES 950 1 ● FIRST_VALUE() 30 MARTIN 1250 2 30 WARD 1250 3 ● LAST_VALUE() 30 TURNER 1500 ● ROW_NUMBER() 30 ALLEN 1600 ● SUM() 30 BLAKE 2850 ● SUM() ● SUM() #Kscope
    6. 6. Choose which rowsDEPTNO ENAME SAL ● SUM() OVER ()------- ------ ----- 10 MILLER 1300 8750 ● SUM() OVER 10 CLARK 2450 2450 (PARTITION BY DEPTNO) 10 KING 5000 7450 ● SUM() OVER ( 20 SMITH 800 10875 20 ADAMS 1100 1100 PARTITION BY DEPTNO 20 JONES 2975 7075 ORDER BY ENAME 20 FORD 3000 4100 ROWS BETWEEN 20 SCOTT 3000 10075 UNBOUNDED PRECEDING 30 JAMES 950 5400 AND CURRENT ROW 30 WARD 1250 8400 ) 30 MARTIN 1250 6650 30 TURNER 1500 7150 30 ALLEN 1600 1600 30 BLAKE 2850 4450 #Kscope
    7. 7. Syntax diagram analytic_function::= analytic_clause::= examples.sql #Kscope
    8. 8. Case/Demo: Top selling items Classic task for a programmer: Show top three by product group Also how big percentage they sold of the total #Kscope
    9. 9. Case/Demo: Top selling items What kind of top three do you wish? ● DENSE_RANK() ● RANK() ● ROW_NUMBER() PARTITION BY groups of items RATIO_TO_REPORT for percentages case1.sql #Kscope
    10. 10. Case/Demo: Picking by FIFO Items stored in different locations in warehouse Pick an order by First-In First-Out principle #Kscope
    11. 11. Case/Demo: Picking by FIFO SUM() by item Ordered by purchase date Rolling sum to find how much was picked by ”previous rows” Filter away rows where sufficient has already been picked case2.sql #Kscope
    12. 12. Case/Demo: Efficient picking route OK, we know what to pick But is this a smart route to drive? #Kscope
    13. 13. Case/Demo: Efficient picking route Wouldn’t this be a smarter route? We need to change direction every other aisle #Kscope
    14. 14. Case/Demo: Efficient picking route What if we lack a door at each end? Direction has to ”restart” per warehouse #Kscope
    15. 15. Case/Demo: Efficient picking route DENSE_RANK() to number the aisles in order visited Order the output ● ”Up” on odd aisles ● ”Down” on even aisles Partition by warehouse if door is missing case3.sql #Kscope
    16. 16. Case/Demo: Picking efficiency How fast can operators pick items? How much do they wait idle for totes to arrive? #Kscope
    17. 17. Case/Demo: Picking efficiency Log over tote missions arriving and departing the picking stations LEAD() on mission log to find the departure following an arrival => picking time LEAD() on mission log to find the arrival following a departure => waiting time case4.sql #Kscope
    18. 18. Case/Demo: Forecasting sales Forecast the sales of next year But follow the trend of the item #Kscope
    19. 19. Case/Demo: Forecasting sales REGR_SLOPE() to calculate trend RANGE window for sliding trend calculation ”Transpose” last years sales by the slope to get next years forecast case5.sql #Kscope
    20. 20. Case/Demo: Forecast zero stock Fireworks sell like crazy last week of December What hour will a store run out of stock? #Kscope
    21. 21. Case/Demo: Forecast zero stock SUM() on budget sales data from ”now” forward Identify hour when rolling sum exceeds stock ● BETWEEN CURRENT ROW AND 1 PRECEDING (Similar technique as picking by FIFO) More than analytics: ● MODEL clause for repeated refill of stock case6.sql #Kscope
    22. 22. The never ending case list We use analytic functions all the time ● WheelGuide® ● Replenish shop stock ● Call Center statistics ● Spare parts guide ● Customer count / work schedule / number of orders ● Booking calendar for mechanics ● Shop space management ● Discover idle hands ● Detect seasonal variations for sales ● Efficiency of Royal Danish Mail We can’t imagine living without analytics  #Kscope
    23. 23. You will find your own cases Just start using analytics The more you do the more often you find cases When you start to think you need to process your data procedurally – think again! Use the power of SQL to let the database do the hard work processing data That’s what the database does best And you’re paying for it so why not use it  #Kscope
    24. 24. Any questions? ● Download from Kscope: ● This presentation ● The scripts used ● A detailed paper ● Or you can get it at: ● http://goo.gl/obxXn @kibeha http://dspsd.blogspot.comPlease remember to fill out evaluation schemas, thank you #Kscope

    ×