16
Getting Started with Oracle 12c Grouping and Aggregating data using SQL Description: BISP is committed to provide BEST learning material to the beginners and advance learners. In the same series, we have prepared a complete end-to end Hands-on Beginner’s Gide !or "racle #nal$tic %nctions. The do cment !ocses Groping and #ggregating data sing S&'  (oin or pro!essional training program and learn !rom e)perts. History: Version Description Change Author Publish Date 0.1 Initial Draft Pawan Maanan 0.1 !e"iew#1 www.bispsolutions.com www.bisptrainigs.com www.hyperionguru.com Page 1

Oracle12c Analytic Functions

Embed Size (px)

Citation preview

Page 1: Oracle12c Analytic Functions

8/13/2019 Oracle12c Analytic Functions

http://slidepdf.com/reader/full/oracle12c-analytic-functions 1/16

Getting Started with Oracle 12c

Grouping and Aggregating data using SQL

Description:BISP is committed to provide BEST learning material to the beginners and advance learners. In

same series, we have prepared a complete end-to end Hands-on Beginner’s Gide !or "racle

#nal$tic %nctions. The docment !ocses Groping and #ggregating data sing S&'

(oin or pro!essional training program and learn !rom e)perts.

History:Version Description Change Author Publish Date0.1 Initial Draft Pawan Maanan0.1 !e"iew#1

www.bispsolutions.com  www.bisptrainigs.com  www.hyperionguru.com  Page 1

Page 2: Oracle12c Analytic Functions

8/13/2019 Oracle12c Analytic Functions

http://slidepdf.com/reader/full/oracle12c-analytic-functions 2/16

Contents:ROLLUP Operator:..............................................................................................................................................3

CUBE Operator :................................................................................................................................................. 5

GROUPING FUNCTION:.................................................................................................................................... 9

GROUPING_ID................................................................................................................................................. 12

GROUPING ET:........................................................................................................................................... 13

CO!POITE COLU!N..................................................................................................................................1"

www.bispsolutions.com  www.bisptrainigs.com  www.hyperionguru.com  Page 2

Page 3: Oracle12c Analytic Functions

8/13/2019 Oracle12c Analytic Functions

http://slidepdf.com/reader/full/oracle12c-analytic-functions 3/16

ROLLUP Operator:

  #$o%t ROLLUP Operator:

• ROLLUP &s an e'tens&on to Gro%p B( )*a%se.

• ROLLUP ena$*es a ELECT state+ent to )a*)%*ate +%*t&p*e *e,e*s o- s%$tota*s a)ross a spe)&-&e

/ro%p o- &+ens&ons.

• Can $e %se $( report +a0ers to e'tra)t stat&st&)s an s%++ar( &n-or+at&on -ro+ te res%*t to %se &n

)arts /raps reports.

• ROLLUP )reates /ro%p&n/ $( +o,&n/ R&/t to Le-t a*on/ te *&st o- )o*%+ns &n GROUP B )*a%se.

• (nta' o- ROLLUP :

  ELECT 4 GROUP B ROLLUP/ro%p&n/_)o*%+n_re-eren)e_*&st6.

To e'e)%te 7%er&es $ase on ROLLUP Operator I a+ /o&n/ to %se ta$*e

  Tables used : order_details

Order_Details $as&)a**( )onta&ns eta&*s o- &--erent orers.

Be-ore %s&n/ ROLLUP operator *ets see 8o*e o- te ata present &n order_details ta$*e.

For 8e 8&** $e e'e)%t&n/ te -o**o8&n/ 7%er( :

Select * fro order_details!

Ten )*&)0 on r%n $%tton spe)&-&e &n $e*o8 &+a/e to e'e)%te te 7%er( .

Output:

Lets sa( 8e 8ant to 0no8 te eta&*s :

Re"uireent:

www.bispsolutions.com  www.bisptrainigs.com  www.hyperionguru.com  Page 3

Page 4: Oracle12c Analytic Functions

8/13/2019 Oracle12c Analytic Functions

http://slidepdf.com/reader/full/oracle12c-analytic-functions 4/16

a6Tota* 7%ant&t( -or e,er( orer on ea) )orrespo&n/ pro%)t s&pp&n/ ate.

$6Tota* 7%ant&t( -or e,er( orer &rrespe)t&,e o- te )orrespon&n/ pro%)t s&pp&n/ ates

)6Gran tota* o- 7%ant&t&es.

;&t a s&n/*e 7%er( e'e)%t&on8e /et a** o- te a$o,e &n-or+at&on . oter8&se 8e 8o%* a,e to %se

+%*t&p*e se*e)t state+ents 8&t UNION #LL tat 8o%* ta0e +%*t&p*e ta$*e a))ess %n*&0e &n ROLL UP

operator on*( s&n/*e a))ess to te $ase ta$*e &s re7%&re.

Quer# :

In te a$o,e 7%er( t$ree /ro%ps 8ere )reate $( ROLLUP Operator &e

%Order_nuber&s$ip_date'&%order_nuber'&%'

Output :

  ())

www.bispsolutions.com  www.bisptrainigs.com  www.hyperionguru.com  Page "

Page 5: Oracle12c Analytic Functions

8/13/2019 Oracle12c Analytic Functions

http://slidepdf.com/reader/full/oracle12c-analytic-functions 5/16

Page 6: Oracle12c Analytic Functions

8/13/2019 Oracle12c Analytic Functions

http://slidepdf.com/reader/full/oracle12c-analytic-functions 6/16

ELECT 4 GROUP B CUBE /ro%p&n/_)o*%+n_re-eren)e_*&st6

To e'e)%te 7%er&es $ase on CUBE Operator I a+ /o&n/ to %se ta$*e

  Tables used : di_products

Di_products $as&)a**( )onta&ns ata a$o%t &--erent pro%)ts

Be-ore %s&n/ 7U19 operator *ets see 8o*e o- te ata present &n di_products ta$*e.

For 8e 8&** $e e'e)%t&n/ te -o**o8&n/ 7%er( so8n &n te &+a/e:

  Ten )*&)0 on r%n $%tton spe)&-&e &n $e*o8 &+a/e to e'e)%te te 7%er( .

Output:

Lets sa( 8e 8ant to 0no8 te -o**o8&n/ eta&*s :

Re"uireent:

a6a,era/e pro%)t&on )ost -or &--erent pro%)ts o- &--erent pro%)t *&ne an o- &--erent pro%)t t(pe

  &e a,era/e $( pro%)t t(pe pro%)t *&ne pro%)t na+e6

$6a,era/e pro%)t&on )ost -or &--erent pro%)ts &rrespe)t&,e o- pro%)t *&ne an pro%)t t(pe

&e a,era/e $( pro%)t na+e6

)6a,era/e pro%)t&on )ost -or &--erent pro%)t t(pe &rrespe)t&,e o- pro%)t *&ne an pro%)t na+e.

  &e a,era/e $( pro%)t t(pe6

6a,era/e pro%)t&on )ost -or &--erent pro%)ts $e*on/&n/ to &--erent pro%)t t(pe &rrespe)t&,e o-

pro%)t *&ne

  &e a,era/e $( pro%)t t(pe an pro%)t na+e6

e6 8o*e a,era/e .;&t a s&n/*e 7%er( e'e)%t&on 8e /et a** o- te a$o,e &n-or+at&on . oter8&se 8e 8o%* a,e to

%se +%*t&p*e se*e)t state+ents 8&t UNION #LL . tat 8o%* ta0e +%*t&p*e ta$*e a))ess %n*&0e &n CUBE

operator on*( s&n/*e a))ess to te $ase ta$*e &s re7%&re.

www.bispsolutions.com  www.bisptrainigs.com  www.hyperionguru.com  Page @

Page 7: Oracle12c Analytic Functions

8/13/2019 Oracle12c Analytic Functions

http://slidepdf.com/reader/full/oracle12c-analytic-functions 7/16

Quer# :

In te a$o,e 7%er( te )%$e operator )reate te -o**o8&n/ /ro%ps:

pro%)t*&nepro%)tt(pepro%)tna+e 6

pro%)t*&ne6

pro%)tt(pe6

pro%)tna+e6

pro%)t*&nepro%)tt(pe6

pro%)t*&nepro%)tna+e6

pro%)tt(pepro%)tna+e6

6Tota* @ sets are )reate $( CUBE operator 8&t tree e*e+ents -ro+ t&s (o% )an &n-er a &re)t -or+%*a &e

For n e*e+entsA)o*%+ns 2nsets 8&** $e -or+e $( te CUBE Operator.

O%tp%t:

(((

www.bispsolutions.com  www.bisptrainigs.com  www.hyperionguru.com  Page

Page 8: Oracle12c Analytic Functions

8/13/2019 Oracle12c Analytic Functions

http://slidepdf.com/reader/full/oracle12c-analytic-functions 8/16

((())

www.bispsolutions.com  www.bisptrainigs.com  www.hyperionguru.com  Page

Page 9: Oracle12c Analytic Functions

8/13/2019 Oracle12c Analytic Functions

http://slidepdf.com/reader/full/oracle12c-analytic-functions 9/16

L&ne *a$e**e as &n&)ates a 8o*e a,era/e an a*so ans8er to o%r 5t re7%&re+ente6

L&ne *a$e**e as + &n&)ates a /ro%p a,era/e $( pro%)t na+e an a*so ans8er to se)on re7%&re+ent-or a

part&)%*ar pro%)t &- (o% 8&** 6 an a*so ans8er to o%r se)on re7%&re+ent$6

L&ne *a$e**e as - &n&)ates a /ro%p a,era/e $( pro%)t t(pe an a*so ans8er to o%r t&r re7%&re+ent)6.

L&ne *a$e**e as &n&)ates a /ro%p a,era/e $( pro%)t t(pe an pro%)t na+e a*so ans8er to o%r -o%rt

re7%&re+ent6.

L&ne *a$e**e as ; &n&)ates a /ro%p a,era/e $( pro%)t *&ne pro%)t t(pe an a*so pro%)tna+e an a*so

ans8er to o%r -&rst re7%&re+enta6.

GROUP6/G <U/7T6O/:

#$o%t /ro%p&n/ -%n)t&on :

• It &s %se 8&t ROLLUP or CUBE operator.

• It &s %se to &ent&-( te )o*%+ns &n a /ro%p -or+&n/ te s%$tota*.

• It &s %se to &st&n/%&s $et8een tore n%**s an te n%**s pro%)e $( ROLLUP or CUBE operator.

• It ret%rns a ,a*%e = or

• (nta':

 GROUPING Co*%+n6

  Use of GROUP6/G <unction :

a6To eter+&ne te )o*%+ns &n,o*,e &n te a//re/at&ons%+a,/+&n6

$6 &ent&-( 8eter a NULL ,a*%e &n a res%*t set &n&)ates :

  ?NULL ,a*%e -ro+ te $ase ta$*e &e store NULL ,a*%e

  ?NULL ,a*%e )reate $( ROLLUP or CUBE.

Return >alue 6ndicates :

I- ret%rn ,a*%e &s ten &t &n&)ates:

?? te )orrespon&n/ )o*%+ns 8as %se to )a*)%*ate a//re/ate ,a*%e

?? NULL ,a*%e &n te e'press&on )o*%+n &s a store NULL ,a*%e

I- ret%rn ,a*%e &s 1 ten &t &n&)ates:

?? te )orrespon&n/ )o*%+ns 8as NOT %se to )a*)%*ate a//re/ate ,a*%e

?? NULL ,a*%e &n te e'press&on )o*%+n &s NOT a store NULL ,a*%e rater &t as $een )reate $( CUBE

or ROLLUP Operator .

 

To e'e)%te 7%er&es $ase on GROUPING Operator I a+ /o&n/ to %se ta$*e

 

www.bispsolutions.com  www.bisptrainigs.com  www.hyperionguru.com  Page 9

Page 10: Oracle12c Analytic Functions

8/13/2019 Oracle12c Analytic Functions

http://slidepdf.com/reader/full/oracle12c-analytic-functions 10/16

  Tables used : di_products

Re"uireent:

a6a,era/e pro%)t&on )ost -or &--erent pro%)ts o- &--erent pro%)t *&ne an o- &--erent pro%)t t(pe

  &e a,era/e $( pro%)t t(pe pro%)t *&ne pro%)t na+e6

$6a,era/e pro%)t&on )ost -or &--erent pro%)ts &rrespe)t&,e o- pro%)t *&ne an pro%)t t(pe

&e a,era/e $( pro%)t na+e6

)6a,era/e pro%)t&on )ost -or &--erent pro%)t t(pe &rrespe)t&,e o- pro%)t *&ne an pro%)t na+e.

  &e a,era/e $( pro%)t t(pe6

6a,era/e pro%)t&on )ost -or &--erent pro%)ts $e*on/&n/ to &--erent pro%)t t(pe &rrespe)t&,e o-

pro%)t *&ne

  &e a,era/e $( pro%)t t(pe an pro%)t na+e6

e6 8o*e a,era/e .

Note : tese are te sa+e re7%&re+ents %se &n te CUBE operator -or te sa+e ta$*e &e &+_pro%)ts .

Te 7%er( %se &n CUBE operator one . as $een +o&-&e to so8 te %se o- /ro%p&n/ -%n)t&ons

www.bispsolutions.com  www.bisptrainigs.com  www.hyperionguru.com  Page 1

Page 11: Oracle12c Analytic Functions

8/13/2019 Oracle12c Analytic Functions

http://slidepdf.com/reader/full/oracle12c-analytic-functions 11/16

Output:

O%tp%t so8n a/a&n:

(()

L&ne *a$e*e as 1 as #ER#GE_COT )o*%+n as ,a*%e 9 8&) &s a,era/e pro%)t&on )ost none o-

te )o*%+ns are &n)*%e &n te a//re/at&on t%s /ro%p&n/ -%n)t&on ret%rns 111 &n G_PRODUCTLINE

G_PRODUCTTPE

G_PRODUCTN#!E .

L&ne *a$e*e as 2 as #ER#GE_COT )o*%+n as ,a*%e 3 to )a*)%*ate t&s ,a*%e on*( productnae

&s &n)*%e &n te a//re/at&on t%s /ro%p&n/ -%n)t&on ret%rns -or G_PRODUCTN#!E an 11 &n

G_PRODUCTLINE G_PRODUCTTPE respe)t&,e*(.

www.bispsolutions.com  www.bisptrainigs.com  www.hyperionguru.com  Page 11

Page 12: Oracle12c Analytic Functions

8/13/2019 Oracle12c Analytic Functions

http://slidepdf.com/reader/full/oracle12c-analytic-functions 12/16

L&ne *a$e*e as 3 as #ER#GE_COT )o*%+n as ,a*%e " to )a*)%*ate t&s ,a*%e $ot productline

productnae 8ere &n)*%e &n te a//re/at&on t%s /ro%p&n/ -%n)t&on ret%rns -or G_PRODUCTN#!E

G_PRODUCTLINE an 1 -or G_PRODUCTTPE respe)t&,e*(.

s)ro** %p to te pre)e&n/ pa/e to see 8at or 1 &n /ro%p&n/ -%n)t&on &n&)ates.6

GROUP6/G_6D•   GROUP6/G_6D -%n)t&on 8or0s as an e'tens&on o- te GROUPING -%n)t&on

• Ro8 -&*ter&n/ &s eas&er 8&t GROUPING_ID $e)a%se te es&re ro8s )an $e &ent&-&e 8&t a s&n/*e

)on&t&on o- GROUPING_ID n.

•  It a))epts one or +ore )o*%+ns an ret%rns te e)&+a* e7%&,a*ent o- te GROUPING $&t ,e)tor  

which is just the combination of results from GROUPING function for each column specified.

• Syntax

GROUPING_ID)o*%+ns6

To e'e)%te 7%er&es $ase on GROUPING_ID Operator I a+ /o&n/ to %se ta$*e

  Tables used : di_products

Te 7%er( %se &n CUBE operator one . as $een +o&-&e to so8 te %se o- /ro%p&n/_ID -%n)t&on:

OUTPUT:

Te GROUPING_ID -%n)t&on ere &s : GID_LINE_TPE_N#!E 8&) &s a)t%a**( )on)atenat&n/ te res%*ts

o- Gro%p&n/ -%n)t&ons &nto a $&t ,e)tor an ten ret%rn&n/ te e)&+a* e7%&,a*ent o- tat .

For e' &n te F&rst Ro8 8ere #ER#GE_COT 9 te /ro%p&n/ -%n)t&on ,a*%es are 1 1 1

)on,ert&n/ &t &nto $&t ,e)tor 111 an te e)&+a* e7%&,a*ent o- t&s &s

www.bispsolutions.com  www.bisptrainigs.com  www.hyperionguru.com  Page 12

Page 13: Oracle12c Analytic Functions

8/13/2019 Oracle12c Analytic Functions

http://slidepdf.com/reader/full/oracle12c-analytic-functions 13/16

In ne't ro8 8e a,e GROUPING -%n)t&on ,a*%es as 11 )on,ert&n/ &t &nto $&t ,e)tor : 11 an &ts

e)&+a* e7%&,a*ent &s : @

/OT9: In te a$o,e o%tp%t I a,e %se 3 /ro%p&n/ -%n)t&ons (o% )an a,o& &t &re)t*( %se GROUPING_ID

-%n)t&on.

o GROUPING_ID a,o&s te %se o- +%*t&p*e GROUPING -%n)t&on an a*so (o% )an +a0e te

-&*ter&n/ )on&t&ons eas&er 8&t te %se s&n/*e GROUPING_ID -%n)t&on &nstea o- +%*t&p*e

GROUPING -%n)t&ons

$( %s&n/ on*( s&n/*e )on&t&on &e GROUPING_ID n

GROUP6/G S9TS:

• F%rter e'tens&on o- GROUP B

• Use to e-&ne +%*t&p*e /ro%p&n/s &n te sa+e

7%er(.

  ?$# to use @ :

•  On*( one ta$*e a))ess &s re7%&re.

• No nee to %se )o+p*e' %n&on 7%er&es.

;&t te e*p o- Gro%p&n/ sets (o% )an spe)&-( te /ro%ps on 8&) te a//re/at&on 8&** $e per-or+e . Un*&0e

&nROLLUP or CUBE 8ere &- I sa( :

ets -or+e Tota* sets

ROLLUPa$6 a$6a6 3 sets

CUBEa$)6 a$)a$)6a$6a)6$)66 23  sets

o te a$o,e sets are a%to+at&)a**( /enerate 8en (o% %se CUBE or ROLLUP $%t +( &nterest +&/t $e &n

so+e o- te /ro%ps t&s &s 8ere (o% )an %se GROUPINGET -%n)t&on to spe)&-( (o%r o8n /ro%ps.

Quer# :

I a,e spe)&-&e on*( 2 /ro%ps so on tose /ro%ps on*( a//re/at&on 8&** $e per-or+e

www.bispsolutions.com  www.bisptrainigs.com  www.hyperionguru.com  Page 13

Page 14: Oracle12c Analytic Functions

8/13/2019 Oracle12c Analytic Functions

http://slidepdf.com/reader/full/oracle12c-analytic-functions 14/16

Output:

L&ne *a$e**e 1 &n&)ates a//re/at&on $( $ot PRODUCTLINE an PRODUCTN#!E as 8e spe)&-&e &n te

GROUPING ET

L&ne *a$e**e 2 &n&)ates a//re/at&on $( PRODUCTN#!E as 8e spe)&-&e &n te GROUPING ET.

7OPOS6T9 7OLU/S

  #$o%t )o+pos&te )o*%+ns :

• Co**e)t&on o- )o*%+ns treate as s&n/*e %n&t.

•  %se &n GROUP B )*a%se.

Bo3 to define coposite coluns @:

•  Use parentes&s to /ro%p )o*%+ns

?$# to use @

•To s0&p a//re/at&on a)ross )erta&n *e,e*s.

• For e'

CUBEa$6)6 a $ to/eter &s a )o+pos&te )o*%+n.

ets -or+e :

ROLLUPa$)66 a$)6 a6

CUBEa$)66 a$)66a$)6

www.bispsolutions.com  www.bisptrainigs.com  www.hyperionguru.com  Page 1"

Page 15: Oracle12c Analytic Functions

8/13/2019 Oracle12c Analytic Functions

http://slidepdf.com/reader/full/oracle12c-analytic-functions 15/16

Quer# :

Output:

4444.

www.bispsolutions.com  www.bisptrainigs.com  www.hyperionguru.com  Page 15

Page 16: Oracle12c Analytic Functions

8/13/2019 Oracle12c Analytic Functions

http://slidepdf.com/reader/full/oracle12c-analytic-functions 16/16

On*( on -o%r /ro%ps a//re/at&on 8as per-or+e $e)a%se 8e a,e spe)&-&e )o+pos&te )o*%+ns 8&) are

treate as s&n/*e %n&toter8&se on +-  /ro%ps a//re/at&on 8o%* a,e $een per-or+e.

Gro%ps -or+e $e)a%se o- )o+pos&te )o*%+ns are :

L&ne *a$e*e $( 1 &n&)ates a//re/at&on $( pro%)t*&ne pro%)tt(pepro%)tna+e

L&ne *a$e*e $( 2 &n&)ates a//re/at&on $( pro%)tt(pe an pro%)tna+e

L&ne *a$e*e $( 3 &n&)ates a//re/at&on $( pro%)t*&ne

L&ne *a$e*e $( " &n&)ates a,era/e )ost o- pro%)t&on_)ost )o*%+n &n te &n &+_pro%)ts ta$*e

 

www.bispsolutions.com  www.bisptrainigs.com  www.hyperionguru.com  Page 1@