Upload
kim-berg-hansen
View
789
Download
4
Tags:
Embed Size (px)
DESCRIPTION
Presentation on Oracle SQL analytic functions as presented by me at ODTUG KScope 2012
Citation preview
#Kscope
Real Cases of Indispensabilityof Analytic Functions
Kim Berg Hansen
T. Hansen Gruppen A/S
#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
#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?
#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
#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
#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
#Kscope
Syntax diagram
examples.sql
analytic_function::=
analytic_clause::=
#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
#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
#Kscope
Case/Demo: Picking by FIFO
Items stored in different locations in warehouse Pick an order by First-In First-Out principle
#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
#Kscope
Case/Demo: Efficient picking route
OK, we know what to pick But is this a smart route to drive?
#Kscope
Case/Demo: Efficient picking route
Wouldn’t this be a smarter route? We need to change direction every other aisle
#Kscope
Case/Demo: Efficient picking route
What if we lack a door at each end? Direction has to ”restart” per warehouse
#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
#Kscope
Case/Demo: Picking efficiency
How fast can operators pick items? How much do they wait idle for totes to arrive?
#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
#Kscope
Case/Demo: Forecasting sales
Forecast the sales of next year But follow the trend of the item
#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
#Kscope
Case/Demo: Forecast zero stock
Fireworks sell like crazy last week of December What hour will a store run out of stock?
#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
#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
#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
#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