1jonathan lewis ukoug dec 2000 analytic functions agenda who am i historical problems the future...
TRANSCRIPT
1Jonathan LewisUKOUG Dec 2000
Analytic Functions
Agenda
Who am I
Historical Problems
The Future
Questions (and answers)
2Jonathan LewisUKOUG Dec 2000
Analytic Functions
Who am I ?
Independent Consultant.
Design, Strategy, Briefing, Training
Reviews and Trouble-shooting
http://www.jlcomp.demon.co.uk
4Jonathan LewisUKOUG Dec 2000
Analytic Functions
History
create table game_sale (
title varchar2(20),
store varchar2(10),
sales number(10,2)
);
5Jonathan LewisUKOUG Dec 2000
Analytic Functions
History
('Portal Combat', 'London', 1824);
('Manic the Gerbil', 'London', 52);
('Age of Umpires', 'London', 110);
('Crash Simulator', 'London', 247);
('Dome', 'London', 2167);
6Jonathan LewisUKOUG Dec 2000
Analytic Functions
History
select
store, title, sales
from
game_sale
order by
store,
sales desc
7Jonathan LewisUKOUG Dec 2000
Analytic Functions
History
Glasgow Crash Simulator 1934 Manic the Gerbil 913 Dome 482 Portal Combat 315 Age of Umpires 72
London Dome 2167 Portal Combat 1824 Crash Simulator 247 Age of Umpires 110 Manic the Gerbil 52
8Jonathan LewisUKOUG Dec 2000
Analytic Functions
History
declarecursor c1 is
select store, title, salesfrom game_saleorder by store, sales desc
;m_last_row c1%rowtype;m_out_ct number := 0;
begindo something . . .
end;
9Jonathan LewisUKOUG Dec 2000
Analytic Functions
History
for r1 in c1 loopif (m_last_row.store != r1.store) then m_out_ct := 0; dbms_output.new_line;end if;
if m_out_ct != 2 then dbms_output.put_line (
r1.store || ' - ' || r1.title || ' - ' || r1.sales
); m_out_ct := m_out_ct + 1;end if;
m_last_row := r1;end loop;
10Jonathan LewisUKOUG Dec 2000
Analytic Functions
History
Glasgow - Crash Simulator - 1934
Glasgow - Manic the Gerbil - 913
London - Dome - 2167
London - Portal Combat - 1824
11Jonathan LewisUKOUG Dec 2000
Analytic Functions
History
('Portal Combat', 'London', 1824);
('Manic the Gerbil', 'London', 52);
('Age of Umpires', 'London', 110);
('Crash Simulator', 'London', 1824);
('Dome', 'London', 2167);
12Jonathan LewisUKOUG Dec 2000
Analytic Functions
History
select store, title, salesfrom game_sale gs1where 2 > (
select count(*)from game_sale gs2where gs2.store = gs1.storeand gs2.sales > gs1.sales)
order bystore, sales desc;
13Jonathan LewisUKOUG Dec 2000
Analytic Functions
History
STORE TITLE SALES
---------- -------------------- ---------
Glasgow Crash Simulator 1934
Manic the Gerbil 913
London Dome 2167
Portal Combat 1824
14Jonathan LewisUKOUG Dec 2000
Analytic Functions
History
('Portal Combat', 'London', 1824);
('Manic the Gerbil', 'London', 52);
('Age of Umpires', 'London', 110);
('Crash Simulator', 'London', 1824);
('Dome', 'London', 2167);
where 2 > (select count(*)
15Jonathan LewisUKOUG Dec 2000
Analytic Functions
The Future.
select
store, title, sales,
rank() over (
partition by store
order by sales desc
) as in_store_rank
from
game_sale
;
16Jonathan LewisUKOUG Dec 2000
Analytic Functions
The Future
Glasgow Crash Simulator 1934 1
Manic the Gerbil 913 2
Dome 482 3
Portal Combat 315 4
Age of Umpires 72 5
London Dome 2167 1
Portal Combat 1824 2
Crash Simulator 247 3
Age of Umpires 110 4
Manic the Gerbil 52 5
17Jonathan LewisUKOUG Dec 2000
Analytic Functions
The Future
select store, title, salesfrom (select store, title, sales,
rank() over (partition by storeorder by sales desc
) as in_store_rankfrom
game_sale)
where in_store_rank <= 2order by store, in_store_rank;
18Jonathan LewisUKOUG Dec 2000
Analytic Functions
The Future
STORE TITLE SALES
---------- -------------------- ---------
Glasgow Crash Simulator 1934
Tonic the Gerbil 913
London Dome 2167
Portal Combat 1824
19Jonathan LewisUKOUG Dec 2000
Analytic Functions
The Future
select store, title, salesfrom (select store, title, sales,
rank() over (partition by storeorder by sales desc
) as in_store_rank,sum(sales) over (
partition by store) as store_totals
from game_sale)where in_store_rank <= 2order bystore_totals desc, in_store_rank;
20Jonathan LewisUKOUG Dec 2000
Analytic Functions
The Future
STORE TITLE SALES
---------- -------------------- ---------
London Dome 2167
Portal Combat 1824
Glasgow Crash Simulator 1934
Tonic the Gerbil 913
21Jonathan LewisUKOUG Dec 2000
Analytic Functions
The Future
AVG SUM** COUNT
MAX MIN
RANK** ROW_NUMBER
DENSE_RANK PERCENT_RANK
22Jonathan LewisUKOUG Dec 2000
Analytic Functions
The Future
RATIO_TO_REPORT
LEAD LAG
FIRST_VALUE LAST_VALUE
NTILE CUME_DIST
23Jonathan LewisUKOUG Dec 2000
Analytic Functions
The Future
VARIANCE STDDEV CORR
VAR_POP VAR_SAMP
COVAR_POP COVAR_SAMP
STDDEV_POP STDDEV_SAMP
REGR_ (linear regression function)
24Jonathan LewisUKOUG Dec 2000
Analytic Functions
The Future
Title Sales Previous Delta
---------------- ------- --------- ------
Crash Simulator 1934 913 1021
Tonic the Gerbil 913 482 431
Dome 482 315 167
Portal Combat 315 72 243
Age of Umpires 72
25Jonathan LewisUKOUG Dec 2000
Analytic Functions
The Future
select title, sales, previous,
this_sale - next_sale delta
from (
select title, sales,
lead(sales,1) over (
partition by store
order by sales desc
) as previous
from
game_sale
);
26Jonathan LewisUKOUG Dec 2000
Analytic Functions
The Future
Store Title Sale St % Co %-------- ----------------- ---- ---- -----Glasgow Crash Simulator 1934 .52 .24 Tonic the Gerbil 913 .25 .11 Dome 482 .13 .06 Portal Combat 315 .08 .04 Age of Umpires 72 .02 .01
London Dome 2167 .49 .27 Portal Combat 1824 .41 .22 Crash Simulator 247 .06 .03 Age of Umpires 110 .03 .01 Tonic the Gerbil 52 .01 .01
27Jonathan LewisUKOUG Dec 2000
Analytic Functions
The Future
select store, title, sales,
ratio_to_report(sales) over (
partition by store
) store_ratio,
ratio_to_report(sales) over (
) country_ratio
from game_sale
order by
store, sales desc;
28Jonathan LewisUKOUG Dec 2000
Analytic Functions
The Future
Store Week Ending Sales Rolling
-------- ----------- ----- -------
Glasgow 07-May-2000 4,000 5,000.0
14-May-2000 5,000 5,500.0
21-May-2000 6,000 6,000.0
28-May-2000 7,000 6,760.4
04-Jun-2000 8,000 7,287.6
11-Jun-2000 7,802 7,714.4
18-Jun-2000 7,636 7,877.4
25-Jun-2000 8,134 7,882.0
2-Jul-2000 7,815 7,902.0
9-Jul-2000 8,023 7,990.7
29Jonathan LewisUKOUG Dec 2000
Analytic Functions
The Future
select
store, week_ending, sales,
avg(sales) over(
partition by store
order by week_ending
range between
14 preceding and 14 following
) rolling_5
from
sales_history
order by
store, week_ending;
30Jonathan LewisUKOUG Dec 2000
Analytic Functions
The Future
Store Week Ending Sales Running
-------- ----------- ----- -------
Glasgow 07-May-2000 4,000 4,000
14-May-2000 5,000 9,500
21-May-2000 6,000 15,000
28-May-2000 7,000 22,000
04-Jun-2000 8,000 30,000
11-Jun-2000 7,802 37,802
18-Jun-2000 7,636 45,438
25-Jun-2000 8,134 53,572
2-Jul-2000 7,815 61,387
9-Jul-2000 8,023 69,410
31Jonathan LewisUKOUG Dec 2000
Analytic Functions
The Future
select
store, week_ending, sales,
avg(sales) over(
partition by store
order by week_ending
range unbounded preceding
) running
from
sales_history
order by
store, week_ending;
32Jonathan LewisUKOUG Dec 2000
Analytic Functions
Warnings
• Lots of sorting.
• Use only on small result sets
• pl/sql doesn’t understand it
• can’t often use view effectively