extreme querying with_analytics

53

Upload: gary-myers

Post on 17-Jun-2015

246 views

Category:

Technology


0 download

DESCRIPTION

Presentation given to the Sydney Oracle meetup on June 30th 2010. Covering Oracle analytics and advanced aggregate functions

TRANSCRIPT

Page 1: Extreme querying with_analytics
Page 2: Extreme querying with_analytics

blah blah NOT LIABLE blah blah blah, I NEVER SAID THAT blah blah READ THE DOCUMENTATION blah blah blah NO PROMISES blah I GET PAID BY THE WORD blah blah

Read my blog at HTTP://BLOG.SYDORACLE.COM

Page 3: Extreme querying with_analytics
Page 4: Extreme querying with_analytics
Page 5: Extreme querying with_analytics
Page 6: Extreme querying with_analytics

Aggregate functions are the basis of many Analytics

All the standard aggregates (MIN, MAX, COUNT, SUM, etc) can be used with analytic clauses.

Page 7: Extreme querying with_analytics

Min / Max (with added KEEP)

KEEP means keep the column value for the highest ranked record.

Page 8: Extreme querying with_analytics

Which of their cities has the most potential slaves ?

Page 9: Extreme querying with_analytics

SYDNEY and X both have a population of 2 million

Page 10: Extreme querying with_analytics

MIN or MAX only makes a difference if there are multiple entries of the same ORDER BY rank

Page 11: Extreme querying with_analytics

Min / Max (with added KEEP) Collect

Create an collection of all the individual values

A list of large cities …

Page 12: Extreme querying with_analytics
Page 13: Extreme querying with_analytics

Min / Max (with added KEEP) Collect XMLAgg (in four steps)

Collect the column(s) into an XML document

Page 14: Extreme querying with_analytics
Page 15: Extreme querying with_analytics
Page 16: Extreme querying with_analytics
Page 17: Extreme querying with_analytics
Page 18: Extreme querying with_analytics

Min / Max (with added KEEP) Collect XMLAGG ListAgg

11g function to create a single VARCHAR2 value from a collection of individual VARCHAR2s

Page 19: Extreme querying with_analytics
Page 20: Extreme querying with_analytics

Wrap the aggregate around a CASE statement to give more aggregation possibilities.

SELECT SUM(case when state='VIC' then pop end)

vic_pop, SUM(case when state='NSW' then pop end)

nsw_pop FROM cities;

Page 21: Extreme querying with_analytics

(at last)

Page 22: Extreme querying with_analytics

Dense Rank / Rank / Row Number

Page 23: Extreme querying with_analytics

Smithers,Bring me a list of our highest paid employees…and the poisoned donuts.

Page 24: Extreme querying with_analytics

select name, wage, sector, row_number() over (partition by sector order by wage

desc) rn, rank() over (partition by sector order by wage desc)

rnk, dense_rank() over (partition by sector order by wage desc)

drnkfrom emporder by sector, wage desc;

Page 25: Extreme querying with_analytics
Page 26: Extreme querying with_analytics
Page 27: Extreme querying with_analytics

Using ROW_NUMBER with other analytics can confuse…

select name, wage, cum_wage from (select name, wage, sum(wage) over (order by wage desc) cwage, row_number() over (order by wage desc) rn from emp where sector = '7G') where rn < 3

NAME WAGE CUM_WAGE Homer 2OO 2OO Lenny 1OO 4OO

Page 28: Extreme querying with_analytics
Page 29: Extreme querying with_analytics

Dense Rank / Rank / Row Number NTILE

The "Snobs" and "Yobs" function

Ignore the outliers and extremes Or ignore the 'huddled masses'

Page 30: Extreme querying with_analytics
Page 31: Extreme querying with_analytics

Exclude the most common 90%

Focus on the most common 10%

Page 32: Extreme querying with_analytics

Dense Rank / Rank / Row Number NTILE Lag / Lead

Look around for the previous or next row

Page 33: Extreme querying with_analytics

MONTH AMOUNT PREV_AMT PERC January 340 February 340 340 .00 March 150 340 -55.88 April 130 150 -13.33 May 170 130 30.77 June 210 170 23.53 July 350 210 66.67 August 270 350 -22.86 September 380 270 40.74

Page 34: Extreme querying with_analytics

MON AMOUNT PREV_AMT ---------- ---------- ---------- January 340 February 340 340 March 150 340 April 130 150 May 170 130 June 170 July 350 170 August 270 350 September 380 270

Page 35: Extreme querying with_analytics

Dense Rank / Rank / Row Number Percent Rank Lag / Lead First / Last

Look further ahead or behind

Page 36: Extreme querying with_analytics

select to_char(period,'Month') mon, amount, first_value(amount) over (partition by trunc(period,'Q') order by period) prev_amt from sales order by period

Page 37: Extreme querying with_analytics

MON AMOUNT PREV_AMT ---------- ---------- ---------- January 340 340 February 340 340 March 150 340 April 130 130 May 170 130 June 210 130 July 350 350 August 270 350 September 380 350

Page 38: Extreme querying with_analytics

Rarely needed in practice Partition By and Order By normally

enough

Page 39: Extreme querying with_analytics

If you omit the PARTITION clause, especially with in-line views , the results can be BAD

Page 40: Extreme querying with_analytics
Page 41: Extreme querying with_analytics
Page 42: Extreme querying with_analytics

In the inline view, the SUM analytic applies to ALL the Orders in the table.

Page 43: Extreme querying with_analytics
Page 44: Extreme querying with_analytics

(if we have time)

Page 45: Extreme querying with_analytics

Rollup Grouping sets Cube

Page 46: Extreme querying with_analytics
Page 47: Extreme querying with_analytics
Page 48: Extreme querying with_analytics

Rollup Cube

CUBE allows combinations of columns to be totaled

Page 49: Extreme querying with_analytics
Page 50: Extreme querying with_analytics

Rollup Cube Grouping sets

Perform grouping across multiple columns Without the lower level totals of CUBE

Page 51: Extreme querying with_analytics
Page 52: Extreme querying with_analytics

If you think you have a problem which the MODEL clause solves then Go have a coffee Go have a bar of chocolate Go have a beer Go have a lie down

BUT do something else until the feeling wears off

Page 53: Extreme querying with_analytics