24
#Kscope Real Cases of Indispensability of Analytic Functions Kim Berg Hansen T. Hansen Gruppen A/S

Real cases of indispensability of Oracle SQL analytic functions

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

Real Cases of Indispensabilityof Analytic Functions

Kim Berg Hansen

T. Hansen Gruppen A/S

Page 2: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

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

Page 3: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

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?

Page 4: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

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

Page 5: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

Access data from other rows

DEPTNO ENAME    SAL------ ------ -----    30 JAMES    950    30 MARTIN  1250    30 WARD    1250    30 TURNER  1500    30 ALLEN   1600    30 BLAKE   2850

● LAG()● LEAD()● FIRST_VALUE()

● SUM()

● ROW_NUMBER()

● LAST_VALUE()

● SUM()● SUM()

123

Page 6: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

Choose which rows

DEPTNO ENAME SAL------- ------ ----- 10 MILLER 1300 10 CLARK 2450 10 KING 5000 20 SMITH 800 20 ADAMS 1100 20 JONES 2975 20 FORD 3000 20 SCOTT 3000 30 JAMES 950 30 WARD 1250 30 MARTIN 1250 30 TURNER 1500 30 ALLEN 1600 30 BLAKE 2850

● SUM() OVER ()

● SUM() OVER(PARTITION BY DEPTNO)

● SUM() OVER ( PARTITION BY DEPTNO ORDER BY ENAME ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)

2450

1100

1600

7450

8750

4100

10875

7075

10075

84005400

71506650

4450

Page 7: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

Syntax diagram

examples.sql

analytic_function::=

analytic_clause::=

Page 8: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

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

Page 9: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

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

Page 10: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

Case/Demo: Picking by FIFO

Items stored in different locations in warehouse Pick an order by First-In First-Out principle

Page 11: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

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

Page 12: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

Case/Demo: Efficient picking route

OK, we know what to pick But is this a smart route to drive?

Page 13: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

Case/Demo: Efficient picking route

Wouldn’t this be a smarter route? We need to change direction every other aisle

Page 14: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

Case/Demo: Efficient picking route

What if we lack a door at each end? Direction has to ”restart” per warehouse

Page 15: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

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

Page 16: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

Case/Demo: Picking efficiency

How fast can operators pick items? How much do they wait idle for totes to arrive?

Page 17: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

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

Page 18: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

Case/Demo: Forecasting sales

Forecast the sales of next year But follow the trend of the item

Page 19: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

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

Page 20: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

Case/Demo: Forecast zero stock

Fireworks sell like crazy last week of December What hour will a store run out of stock?

Page 21: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

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

Page 22: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

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

Page 23: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

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

Page 24: Real cases of indispensability of Oracle SQL analytic functions

#Kscope

Any questions?

● Download from Kscope:● This presentation● The scripts used● A detailed paper

● Or you can get it at:● http://goo.gl/obxXn

@kibehahttp://dspsd.blogspot.com

P l e a s e r e m e m b e r t o f i l l o u t e v a l u a t i o n s c h e m a s , t h a n k y o u