extreme querying with analytics

53
EXTREME QUERYING WITH ANALYTICS

Upload: ganya

Post on 23-Feb-2016

47 views

Category:

Documents


0 download

DESCRIPTION

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. DISCLAIMER. Read my blog at HTTP://BLOG.SYDORACLE.COM. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Extreme Querying   With analytics

EXTREME QUERYING WITH ANALYTICS

Page 2: Extreme Querying   With analytics

DISCLAIMERblah 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

PARTITIONS AND WINDOWS

Page 4: Extreme Querying   With analytics

A ROW IN A RESULT SET WAS ALL ALONE…

Page 5: Extreme Querying   With analytics

IT CAN GET CHAOTIC

Page 6: Extreme Querying   With analytics

AGGREGATE FUNCTIONS 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

NEW AGGREGATE FUNCTIONS Min / Max (with added KEEP)

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

Page 8: Extreme Querying   With analytics

*THE MOST POPULOUS CITY

Which of their cities has the most potential slaves ?

Page 9: Extreme Querying   With analytics

CITIES DATA

SYDNEY and X both have a population of 2 million

Page 10: Extreme Querying   With analytics

RESULTS

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

Page 11: Extreme Querying   With analytics

AGGREGATE FUNCTIONS 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

RESULTS

Page 13: Extreme Querying   With analytics

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

Collect the column(s) into an XML document

Page 14: Extreme Querying   With analytics

1. RECORD TO XML FRAGMENT

Page 15: Extreme Querying   With analytics

2. 'PARENT' ELEMENT FOR FRAGMENT

Page 16: Extreme Querying   With analytics

3. AGGREGATE XML FRAGMENTS

Page 17: Extreme Querying   With analytics

4. TOP-LEVEL PARENT FOR AGGREGATION

Page 18: Extreme Querying   With analytics

AGGREGATE FUNCTIONS 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

LISTAGG - 11G

Page 20: Extreme Querying   With analytics

AGGREGATE FUNCTIONS WITH CASE 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

ANALYTIC FUNCTIONS(at last)

Page 22: Extreme Querying   With analytics

AGGREGATE FUNCTIONS FOR ANALYTICS Dense Rank / Rank / Row Number

Page 23: Extreme Querying   With analytics

ANALYTIC FUNCTIONS

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

Page 24: Extreme Querying   With analytics

AGGREGATE FUNCTIONS FOR ANALYTICSselect 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

AGGREGATE FUNCTIONS FOR ANALYTICS

Page 26: Extreme Querying   With analytics

SUM - WITH ANALYTICS

Page 27: Extreme Querying   With analytics

WARNING 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

SOLUTION : SWITCH WINDOWING TO ROWS

Page 29: Extreme Querying   With analytics

AGGREGATE FUNCTIONS FOR 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

NORMALLY DISTRIBUTED DATA

Page 31: Extreme Querying   With analytics

NTILEExclude the most common 90%

Focus on the most common 10%

Page 32: Extreme Querying   With analytics

AGGREGATE FUNCTIONS FOR ANALYTICS Dense Rank / Rank / Row Number NTILE Lag / Lead

Look around for the previous or next row

Page 33: Extreme Querying   With analytics

LAG - PERCENTAGE CHANGE OVER TIME 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

LAG - IGNORE NULLS (11G) 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

AGGREGATE FUNCTIONS FOR ANALYTICS Dense Rank / Rank / Row Number Percent Rank Lag / Lead First / Last

Look further ahead or behind

Page 36: Extreme Querying   With analytics

FIRST_VALUE AND PARTITION BY 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

RESULTS 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

WINDOW CLAUSE Rarely needed in practice Partition By and Order By normally

enough

Page 39: Extreme Querying   With analytics

BEWARE THE MISSING PARTITION If you omit the PARTITION clause,

especially with in-line views , the results can be BAD

Page 40: Extreme Querying   With analytics

MULTIPLE LINES FOR MULTIPLE ORDERS

Page 41: Extreme Querying   With analytics

REGULAR QUERY WITH ANALYTIC

Page 42: Extreme Querying   With analytics

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

Page 43: Extreme Querying   With analytics

INCLUDE A PARTITION BY CLAUSE

Page 44: Extreme Querying   With analytics

ADVANCED GROUPINGS

(if we have time)

Page 45: Extreme Querying   With analytics

TOTALS AND SUBTOTALS Rollup Grouping sets Cube

Page 46: Extreme Querying   With analytics

ROLLUP, ROLLUP

Page 47: Extreme Querying   With analytics

TOTALS AND SUBTOTALS

Page 48: Extreme Querying   With analytics

TOTALS AND SUBTOTALS Rollup Cube

CUBE allows combinations of columns to be totaled

Page 49: Extreme Querying   With analytics

CUBE - EXAMPLE

Page 50: Extreme Querying   With analytics

TOTALS AND SUBTOTALS Rollup Cube Grouping sets

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

Page 51: Extreme Querying   With analytics

GROUPING SETS

Page 52: Extreme Querying   With analytics

MODEL CLAUSE 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

PIVOT / UNPIVOT IN 11G