SlideShare une entreprise Scribd logo
1  sur  28
Télécharger pour lire hors ligne
OLAP extensions to the SQL standard.
   Advantages of SQL include that it is easy to
    learn, non-procedural, free-format, DBMS-
    independent, and that it is a recognized
    international standard.
   However, major limitation of SQL is the
    inability to answer routinely asked business
    queries such as computing the percentage
    change in values between this month and a
    year ago or to compute moving averages,
    cumulative sums, and other statistical
    functions.
2
   Answer is ANSI adopted a set of OLAP functions as an
    extension to SQL to enable these calculations as well as
    many others that used to be impossible or even
    impractical within SQL.
   IBM and Oracle jointly proposed these extensions early in
    1999 and they now form part of the current SQL standard,
    namely SQL: 2003.
   The extensions are collectively referred to as the ‘OLAP
    package’ and are described as follows:
     Feature T431, ‘Extended Grouping capabilities’
     Feature T611, ‘Extended OLAP operators’


3
   Aggregation is a fundamental part of
        OLAP. To improve aggregation capabilities
        the SQL standard provides extensions to
        the GROUP BY clause such as the ROLLUP
        and CUBE functions.




4
   ROLLUP supports calculations using aggregations such
        as SUM, COUNT, MAX, MIN, and AVG at increasing levels
        of aggregation, from the most detailed up to a grand
        total.
       CUBE is similar to ROLLUP, enabling a single statement
        to calculate all possible combinations of aggregations.
        CUBE can generate the information needed in cross-
        tabulation reports with a single query.
       ROLLUP and CUBE extensions specify exactly the
        groupings of interest in the GROUP BY clause and
        produces a single result set that is equivalent to a UNION
        ALL of differently grouped rows.



5
   ROLLUP Extension to GROUP BY
     enables a SELECT statement to calculate
     multiple levels of subtotals across a specified
     group of dimensions. ROLLUP appears in the
     GROUP BY clause in a SELECT statement using
     the following format:

    SELECT ... GROUP BY ROLLUP(columnList)

6
7
   ROLLUP creates
    subtotals that roll
    up from the most
    detailed level to a
    grand total,
    following a
    column list
    specified in the
    ROLLUP clause.



        ROLLUP(propertyType, yearMonth, city)
    8
   ROLLUP first calculates the standard
        aggregate values specified in the GROUP
        BY clause and then creates progressively
        higher level subtotals, moving from right
        to left through the column list until finally
        completing with a grand total.

           ROLLUP(propertyType, yearMonth, city)



9
 ROLLUP creates subtotals at n + 1 levels,
      where n is the number of grouping columns.
      For instance, if a query specifies ROLLUP on
      grouping columns of propertyType,
      yearMonth, and city (n = 3), the result set will
      include rows at 4 aggregation levels.




10
   Show the totals for sales of flats or houses by branch
         offices located in Aberdeen, Edinburgh, or Glasgow for the
         months of September and October of 2004.
           SELECT propertyType, yearMonth, city, SUM(saleAmount) AS sales
           FROM Branch, PropertyFor Sale, PropertySale
           WHERE Branch.branchNo = PropertySale.branchNo
           AND PropertyForSale.propertyNo = PropertySale.propertyNo
           AND PropertySale.yearMonth IN ('2004-08', '2004-09')
           AND Branch.city IN (‘Aberdeen’, ‘Edinburgh’, ‘Glasgow’)
            GROUP BY ROLLUP(propertyType, yearMonth, city);




11
blanks are
     NULL
     values




12
   CUBE Extension to GROUP BY
      CUBE takes a specified set of grouping
      columns and creates subtotals for all of the
      possible combinations. CUBE appears in the
      GROUP BY clause in a SELECT statement
      using the following format:

     SELECT ... GROUP BY CUBE(columnList)


13
    CUBE
     generates all
     the subtotals
     that could be
     calculated for
     a data cube
     with the
     specified
     dimensions.
14
   CUBE can be used in any situation requiring
     cross-tabular reports. The data needed for
     cross-tabular reports can be generated with
     a single SELECT using CUBE. Like ROLLUP,
     CUBE can be helpful in generating summary
     tables.




15
   CUBE is typically most suitable in
     queries that use columns from multiple
     dimensions rather than columns
     representing different levels of a single
     dimension.




16
   Show all possible subtotals for sales of properties by
     branches offices in Aberdeen, Edinburgh, and Glasgow
     for the months of September and October of 2004.
       SELECT propertyType, yearMonth, city, SUM(saleAmount) AS sales
       FROM Branch, PropertyFor Sale, PropertySale
       WHERE Branch.branchNo = PropertySale.branchNo
       AND      PropertyForSale.propertyNo = PropertySale.propertyNo
       AND PropertySale.yearMonth IN ('2004-08', '2004-09')
       AND Branch.city IN (‘Aberdeen’, ‘Edinburgh’, ‘Glasgow’)
        GROUP BY CUBE(propertyType, yearMonth, city);




17
18
   The function grouping() can be applied on
     an attribute
      Returns 1 if the value is a null value representing
       all, and returns 0 in all other cases.
     select item-name, color, size, sum(number),
        grouping(item-name) as item-name-flag,
        grouping(color) as color-flag,
        grouping(size) as size-flag,
     from sales
     group by cube(item-name, color, size)
19
   Supports a variety of operations such as
     rankings and window calculations.
    Ranking functions include cumulative
     distributions, percent rank, and N-tiles.
    Windowing allows the calculation of
     cumulative and moving aggregations
     using functions such as SUM, AVG, MIN,
     and COUNT.


20
   Ranking Functions
      Computes the rank of a record compared to
      other records in the dataset based on the
      values of a set of measures.




21
    Rank the total sales of properties for branch
     offices in Edinburgh.
         SELECT branchNo,
                 SUM(saleAmount) AS sales,
                 RANK() OVER (ORDER BY SUM(saleAmount)) DESC
                          AS ranking,
                 DENSE_RANK() OVER (ORDER BY SUM(saleAmount)) DESC
                          AS dense_ranking
         FROM Branch, PropertySale
         WHERE Branch.branchNo = PropertySale.branchNo
         AND Branch.city = ‘Edinburgh’
         GROUP BY(branchNo);


    22
23
 There are various types of ranking functions,
   including RANK and DENSE_RANK.
  The syntax for each ranking function is:
   RANK( ) OVER (ORDER BY columnList)
   DENSE_RANK( ) OVER (ORDER BY columnList)
  The difference between RANK and DENSE_RANK
   is that DENSE_RANK leaves no gaps in the
   sequential ranking sequence when there are ties
   for a ranking.


24
   Windowing Calculations
      Can be used to compute cumulative, moving,
       and centered aggregates. They return a value
       for each row in the table, which depends on
       other rows in the corresponding window.
      These aggregate functions provide access to
       more than one row of a table without a self-
       join and can be used only in the SELECT and
       ORDER BY clauses of the query.



25
    Show the monthly figures and three-month moving
     averages and sums for property sales at branch office B003
     for the first six months of 2004.
       SELECT   yearMonth,
                SUM(saleAmount) AS monthlySales,
                AVG(SUM(saleAmount))
                         OVER (ORDER BY yearMonth, ROWS 2 PRECEDING)
                                 AS 3-month moving avg,
                SUM(SUM(salesAmount))
                         OVER (ORDER BY yearMonth ROWS 2 PRECEDING)
                                 AS 3-month moving sum
       FROM PropertySale
       WHERE branchNo = ‘B003’
       AND yearMonth BETWEEN ('2004-01' AND '2004-06’)
       GROUP BY yearMonth
       ORDER BY yearMonth;

26
+




     Windowing = 2
     210000+350000+400000 = 960000 / 3 = 320000
27
    RATIO: Proportion of a value over the total
     set of values
      calculates the ratio of an employee's salary over the
       sum of salaries in his department

                                     2300+2500 = 4800
                                     2300/4800 = 0,479166




28

Contenu connexe

Tendances

Advanced PLSQL Optimizing for Better Performance
Advanced PLSQL Optimizing for Better PerformanceAdvanced PLSQL Optimizing for Better Performance
Advanced PLSQL Optimizing for Better PerformanceZohar Elkayam
 
Les03 (Using Single Row Functions To Customize Output)
Les03 (Using Single Row Functions To Customize Output)Les03 (Using Single Row Functions To Customize Output)
Les03 (Using Single Row Functions To Customize Output)Achmad Solichin
 
Using single row functions to customize output
Using single row functions to customize outputUsing single row functions to customize output
Using single row functions to customize outputSyed Zaid Irshad
 
1. dml select statement reterive data
1. dml select statement reterive data1. dml select statement reterive data
1. dml select statement reterive dataAmrit Kaur
 
What's New in MariaDB Server 10.3
What's New in MariaDB Server 10.3What's New in MariaDB Server 10.3
What's New in MariaDB Server 10.3MariaDB plc
 
Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Achmad Solichin
 
Part3 Explain the Explain Plan
Part3 Explain the Explain PlanPart3 Explain the Explain Plan
Part3 Explain the Explain PlanMaria Colgan
 
COIS 420 - Practice 03
COIS 420 - Practice 03COIS 420 - Practice 03
COIS 420 - Practice 03Angel G Diaz
 
Efficient spatial queries on vanilla databases
Efficient spatial queries on vanilla databasesEfficient spatial queries on vanilla databases
Efficient spatial queries on vanilla databasesJulian Hyde
 
Single-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSingle-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSalman Memon
 
MYSQL single rowfunc-multirowfunc-groupby-having
MYSQL single rowfunc-multirowfunc-groupby-havingMYSQL single rowfunc-multirowfunc-groupby-having
MYSQL single rowfunc-multirowfunc-groupby-havingAhmed Farag
 
Chetan postgresql partitioning
Chetan postgresql partitioningChetan postgresql partitioning
Chetan postgresql partitioningOpenSourceIndia
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL QueriesAchievers Tech
 
Window functions with SQL Server 2016
Window functions with SQL Server 2016Window functions with SQL Server 2016
Window functions with SQL Server 2016Mark Tabladillo
 

Tendances (20)

Advanced PLSQL Optimizing for Better Performance
Advanced PLSQL Optimizing for Better PerformanceAdvanced PLSQL Optimizing for Better Performance
Advanced PLSQL Optimizing for Better Performance
 
Les03 (Using Single Row Functions To Customize Output)
Les03 (Using Single Row Functions To Customize Output)Les03 (Using Single Row Functions To Customize Output)
Les03 (Using Single Row Functions To Customize Output)
 
Single row functions
Single row functionsSingle row functions
Single row functions
 
Using single row functions to customize output
Using single row functions to customize outputUsing single row functions to customize output
Using single row functions to customize output
 
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek SharmaIntroduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
 
1. dml select statement reterive data
1. dml select statement reterive data1. dml select statement reterive data
1. dml select statement reterive data
 
R Get Started I
R Get Started IR Get Started I
R Get Started I
 
R Get Started II
R Get Started IIR Get Started II
R Get Started II
 
What's New in MariaDB Server 10.3
What's New in MariaDB Server 10.3What's New in MariaDB Server 10.3
What's New in MariaDB Server 10.3
 
Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)
 
Part3 Explain the Explain Plan
Part3 Explain the Explain PlanPart3 Explain the Explain Plan
Part3 Explain the Explain Plan
 
COIS 420 - Practice 03
COIS 420 - Practice 03COIS 420 - Practice 03
COIS 420 - Practice 03
 
Efficient spatial queries on vanilla databases
Efficient spatial queries on vanilla databasesEfficient spatial queries on vanilla databases
Efficient spatial queries on vanilla databases
 
Single-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSingle-Row Functions in orcale Data base
Single-Row Functions in orcale Data base
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
 
MYSQL single rowfunc-multirowfunc-groupby-having
MYSQL single rowfunc-multirowfunc-groupby-havingMYSQL single rowfunc-multirowfunc-groupby-having
MYSQL single rowfunc-multirowfunc-groupby-having
 
Chetan postgresql partitioning
Chetan postgresql partitioningChetan postgresql partitioning
Chetan postgresql partitioning
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL Queries
 
Les03
Les03Les03
Les03
 
Window functions with SQL Server 2016
Window functions with SQL Server 2016Window functions with SQL Server 2016
Window functions with SQL Server 2016
 

En vedette

Olap Functions Suport in Informix
Olap Functions Suport in InformixOlap Functions Suport in Informix
Olap Functions Suport in InformixBingjie Miao
 
IBM DB2 Analytics Accelerator Trends & Directions by Namik Hrle
IBM DB2 Analytics Accelerator  Trends & Directions by Namik Hrle IBM DB2 Analytics Accelerator  Trends & Directions by Namik Hrle
IBM DB2 Analytics Accelerator Trends & Directions by Namik Hrle Surekha Parekh
 
OLAP Basics and Fundamentals by Bharat Kalia
OLAP Basics and Fundamentals by Bharat Kalia OLAP Basics and Fundamentals by Bharat Kalia
OLAP Basics and Fundamentals by Bharat Kalia Bharat Kalia
 
Using olap
Using olapUsing olap
Using olapAnne Lee
 

En vedette (6)

Olap Functions Suport in Informix
Olap Functions Suport in InformixOlap Functions Suport in Informix
Olap Functions Suport in Informix
 
Pl sql
Pl sqlPl sql
Pl sql
 
IBM DB2 Analytics Accelerator Trends & Directions by Namik Hrle
IBM DB2 Analytics Accelerator  Trends & Directions by Namik Hrle IBM DB2 Analytics Accelerator  Trends & Directions by Namik Hrle
IBM DB2 Analytics Accelerator Trends & Directions by Namik Hrle
 
OLAP Basics and Fundamentals by Bharat Kalia
OLAP Basics and Fundamentals by Bharat Kalia OLAP Basics and Fundamentals by Bharat Kalia
OLAP Basics and Fundamentals by Bharat Kalia
 
Using olap
Using olapUsing olap
Using olap
 
Types dbms
Types dbmsTypes dbms
Types dbms
 

Similaire à Olapsql

Oracle_Analytical_function.pdf
Oracle_Analytical_function.pdfOracle_Analytical_function.pdf
Oracle_Analytical_function.pdfKalyankumarVenkat1
 
OBIEE 12c Advanced Analytic Functions
OBIEE 12c Advanced Analytic FunctionsOBIEE 12c Advanced Analytic Functions
OBIEE 12c Advanced Analytic FunctionsMichael Perhats
 
Olap fundamentals
Olap fundamentalsOlap fundamentals
Olap fundamentalsAmit Sharma
 
Intro to Data warehousing Lecture 06
Intro to Data warehousing   Lecture 06Intro to Data warehousing   Lecture 06
Intro to Data warehousing Lecture 06AnwarrChaudary
 
Cubing and Metrics in SQL, oh my!
Cubing and Metrics in SQL, oh my!Cubing and Metrics in SQL, oh my!
Cubing and Metrics in SQL, oh my!Julian Hyde
 
BI Portfolio
BI PortfolioBI Portfolio
BI Portfoliopstaerker
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence PortfolioChris Seebacher
 
Fun with ClickHouse Window Functions-2021-08-19.pdf
Fun with ClickHouse Window Functions-2021-08-19.pdfFun with ClickHouse Window Functions-2021-08-19.pdf
Fun with ClickHouse Window Functions-2021-08-19.pdfAltinity Ltd
 
Fun with click house window functions webinar slides 2021-08-19
Fun with click house window functions webinar slides  2021-08-19Fun with click house window functions webinar slides  2021-08-19
Fun with click house window functions webinar slides 2021-08-19Altinity Ltd
 

Similaire à Olapsql (20)

Oracle_Analytical_function.pdf
Oracle_Analytical_function.pdfOracle_Analytical_function.pdf
Oracle_Analytical_function.pdf
 
OBIEE 12c Advanced Analytic Functions
OBIEE 12c Advanced Analytic FunctionsOBIEE 12c Advanced Analytic Functions
OBIEE 12c Advanced Analytic Functions
 
Sql wksht-3
Sql wksht-3Sql wksht-3
Sql wksht-3
 
DWO -Pertemuan 1
DWO -Pertemuan 1DWO -Pertemuan 1
DWO -Pertemuan 1
 
Olap fundamentals
Olap fundamentalsOlap fundamentals
Olap fundamentals
 
Intro to Data warehousing Lecture 06
Intro to Data warehousing   Lecture 06Intro to Data warehousing   Lecture 06
Intro to Data warehousing Lecture 06
 
Cubing and Metrics in SQL, oh my!
Cubing and Metrics in SQL, oh my!Cubing and Metrics in SQL, oh my!
Cubing and Metrics in SQL, oh my!
 
BI Portfolio
BI PortfolioBI Portfolio
BI Portfolio
 
SQL Tips Calculate Running Totals.pptx
SQL Tips Calculate Running Totals.pptxSQL Tips Calculate Running Totals.pptx
SQL Tips Calculate Running Totals.pptx
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence Portfolio
 
Oracle: Functions
Oracle: FunctionsOracle: Functions
Oracle: Functions
 
Oracle: Functions
Oracle: FunctionsOracle: Functions
Oracle: Functions
 
Les04
Les04Les04
Les04
 
Fun with ClickHouse Window Functions-2021-08-19.pdf
Fun with ClickHouse Window Functions-2021-08-19.pdfFun with ClickHouse Window Functions-2021-08-19.pdf
Fun with ClickHouse Window Functions-2021-08-19.pdf
 
Fun with click house window functions webinar slides 2021-08-19
Fun with click house window functions webinar slides  2021-08-19Fun with click house window functions webinar slides  2021-08-19
Fun with click house window functions webinar slides 2021-08-19
 
Presentation groupby
Presentation groupbyPresentation groupby
Presentation groupby
 
Excel formula
Excel formula Excel formula
Excel formula
 
ADV PPT 8 LAB.pptx
ADV PPT 8 LAB.pptxADV PPT 8 LAB.pptx
ADV PPT 8 LAB.pptx
 
LCC Asia Pacific_financialmodelling_excelshortcuts1
LCC Asia Pacific_financialmodelling_excelshortcuts1LCC Asia Pacific_financialmodelling_excelshortcuts1
LCC Asia Pacific_financialmodelling_excelshortcuts1
 
Excel.useful fns
Excel.useful fnsExcel.useful fns
Excel.useful fns
 

Plus de Claudia Gomez

Plus de Claudia Gomez (20)

3 olap storage
3 olap storage3 olap storage
3 olap storage
 
3 olap storage
3 olap storage3 olap storage
3 olap storage
 
2 olap operaciones
2 olap operaciones2 olap operaciones
2 olap operaciones
 
1 introba
1 introba1 introba
1 introba
 
Diseño fisico particiones_3
Diseño fisico particiones_3Diseño fisico particiones_3
Diseño fisico particiones_3
 
Diseño fisico indices_2
Diseño fisico indices_2Diseño fisico indices_2
Diseño fisico indices_2
 
Diseño fisico 1
Diseño fisico 1Diseño fisico 1
Diseño fisico 1
 
Agreggates iii
Agreggates iiiAgreggates iii
Agreggates iii
 
Agreggates ii
Agreggates iiAgreggates ii
Agreggates ii
 
Agreggates i
Agreggates iAgreggates i
Agreggates i
 
Dw design hierarchies_7
Dw design hierarchies_7Dw design hierarchies_7
Dw design hierarchies_7
 
Dw design fact_tables_types_6
Dw design fact_tables_types_6Dw design fact_tables_types_6
Dw design fact_tables_types_6
 
Dw design date_dimension_1_1
Dw design date_dimension_1_1Dw design date_dimension_1_1
Dw design date_dimension_1_1
 
Dw design 4_bus_architecture
Dw design 4_bus_architectureDw design 4_bus_architecture
Dw design 4_bus_architecture
 
Dw design 3_surro_keys
Dw design 3_surro_keysDw design 3_surro_keys
Dw design 3_surro_keys
 
Dw design 2_conceptual_model
Dw design 2_conceptual_modelDw design 2_conceptual_model
Dw design 2_conceptual_model
 
Dw design 1_dim_facts
Dw design 1_dim_factsDw design 1_dim_facts
Dw design 1_dim_facts
 
3 dw architectures
3 dw architectures3 dw architectures
3 dw architectures
 
2 dw requeriments
2 dw requeriments2 dw requeriments
2 dw requeriments
 
1 dw projectplanning
1 dw projectplanning1 dw projectplanning
1 dw projectplanning
 

Olapsql

  • 1. OLAP extensions to the SQL standard.
  • 2. Advantages of SQL include that it is easy to learn, non-procedural, free-format, DBMS- independent, and that it is a recognized international standard.  However, major limitation of SQL is the inability to answer routinely asked business queries such as computing the percentage change in values between this month and a year ago or to compute moving averages, cumulative sums, and other statistical functions. 2
  • 3. Answer is ANSI adopted a set of OLAP functions as an extension to SQL to enable these calculations as well as many others that used to be impossible or even impractical within SQL.  IBM and Oracle jointly proposed these extensions early in 1999 and they now form part of the current SQL standard, namely SQL: 2003.  The extensions are collectively referred to as the ‘OLAP package’ and are described as follows:  Feature T431, ‘Extended Grouping capabilities’  Feature T611, ‘Extended OLAP operators’ 3
  • 4. Aggregation is a fundamental part of OLAP. To improve aggregation capabilities the SQL standard provides extensions to the GROUP BY clause such as the ROLLUP and CUBE functions. 4
  • 5. ROLLUP supports calculations using aggregations such as SUM, COUNT, MAX, MIN, and AVG at increasing levels of aggregation, from the most detailed up to a grand total.  CUBE is similar to ROLLUP, enabling a single statement to calculate all possible combinations of aggregations. CUBE can generate the information needed in cross- tabulation reports with a single query.  ROLLUP and CUBE extensions specify exactly the groupings of interest in the GROUP BY clause and produces a single result set that is equivalent to a UNION ALL of differently grouped rows. 5
  • 6. ROLLUP Extension to GROUP BY  enables a SELECT statement to calculate multiple levels of subtotals across a specified group of dimensions. ROLLUP appears in the GROUP BY clause in a SELECT statement using the following format: SELECT ... GROUP BY ROLLUP(columnList) 6
  • 7. 7
  • 8. ROLLUP creates subtotals that roll up from the most detailed level to a grand total, following a column list specified in the ROLLUP clause. ROLLUP(propertyType, yearMonth, city) 8
  • 9. ROLLUP first calculates the standard aggregate values specified in the GROUP BY clause and then creates progressively higher level subtotals, moving from right to left through the column list until finally completing with a grand total. ROLLUP(propertyType, yearMonth, city) 9
  • 10.  ROLLUP creates subtotals at n + 1 levels, where n is the number of grouping columns. For instance, if a query specifies ROLLUP on grouping columns of propertyType, yearMonth, and city (n = 3), the result set will include rows at 4 aggregation levels. 10
  • 11. Show the totals for sales of flats or houses by branch offices located in Aberdeen, Edinburgh, or Glasgow for the months of September and October of 2004. SELECT propertyType, yearMonth, city, SUM(saleAmount) AS sales FROM Branch, PropertyFor Sale, PropertySale WHERE Branch.branchNo = PropertySale.branchNo AND PropertyForSale.propertyNo = PropertySale.propertyNo AND PropertySale.yearMonth IN ('2004-08', '2004-09') AND Branch.city IN (‘Aberdeen’, ‘Edinburgh’, ‘Glasgow’) GROUP BY ROLLUP(propertyType, yearMonth, city); 11
  • 12. blanks are NULL values 12
  • 13. CUBE Extension to GROUP BY  CUBE takes a specified set of grouping columns and creates subtotals for all of the possible combinations. CUBE appears in the GROUP BY clause in a SELECT statement using the following format: SELECT ... GROUP BY CUBE(columnList) 13
  • 14. CUBE generates all the subtotals that could be calculated for a data cube with the specified dimensions. 14
  • 15. CUBE can be used in any situation requiring cross-tabular reports. The data needed for cross-tabular reports can be generated with a single SELECT using CUBE. Like ROLLUP, CUBE can be helpful in generating summary tables. 15
  • 16. CUBE is typically most suitable in queries that use columns from multiple dimensions rather than columns representing different levels of a single dimension. 16
  • 17. Show all possible subtotals for sales of properties by branches offices in Aberdeen, Edinburgh, and Glasgow for the months of September and October of 2004. SELECT propertyType, yearMonth, city, SUM(saleAmount) AS sales FROM Branch, PropertyFor Sale, PropertySale WHERE Branch.branchNo = PropertySale.branchNo AND PropertyForSale.propertyNo = PropertySale.propertyNo AND PropertySale.yearMonth IN ('2004-08', '2004-09') AND Branch.city IN (‘Aberdeen’, ‘Edinburgh’, ‘Glasgow’) GROUP BY CUBE(propertyType, yearMonth, city); 17
  • 18. 18
  • 19. The function grouping() can be applied on an attribute  Returns 1 if the value is a null value representing all, and returns 0 in all other cases. select item-name, color, size, sum(number), grouping(item-name) as item-name-flag, grouping(color) as color-flag, grouping(size) as size-flag, from sales group by cube(item-name, color, size) 19
  • 20. Supports a variety of operations such as rankings and window calculations.  Ranking functions include cumulative distributions, percent rank, and N-tiles.  Windowing allows the calculation of cumulative and moving aggregations using functions such as SUM, AVG, MIN, and COUNT. 20
  • 21. Ranking Functions  Computes the rank of a record compared to other records in the dataset based on the values of a set of measures. 21
  • 22. Rank the total sales of properties for branch offices in Edinburgh. SELECT branchNo, SUM(saleAmount) AS sales, RANK() OVER (ORDER BY SUM(saleAmount)) DESC AS ranking, DENSE_RANK() OVER (ORDER BY SUM(saleAmount)) DESC AS dense_ranking FROM Branch, PropertySale WHERE Branch.branchNo = PropertySale.branchNo AND Branch.city = ‘Edinburgh’ GROUP BY(branchNo); 22
  • 23. 23
  • 24.  There are various types of ranking functions, including RANK and DENSE_RANK.  The syntax for each ranking function is: RANK( ) OVER (ORDER BY columnList) DENSE_RANK( ) OVER (ORDER BY columnList)  The difference between RANK and DENSE_RANK is that DENSE_RANK leaves no gaps in the sequential ranking sequence when there are ties for a ranking. 24
  • 25. Windowing Calculations  Can be used to compute cumulative, moving, and centered aggregates. They return a value for each row in the table, which depends on other rows in the corresponding window.  These aggregate functions provide access to more than one row of a table without a self- join and can be used only in the SELECT and ORDER BY clauses of the query. 25
  • 26. Show the monthly figures and three-month moving averages and sums for property sales at branch office B003 for the first six months of 2004. SELECT yearMonth, SUM(saleAmount) AS monthlySales, AVG(SUM(saleAmount)) OVER (ORDER BY yearMonth, ROWS 2 PRECEDING) AS 3-month moving avg, SUM(SUM(salesAmount)) OVER (ORDER BY yearMonth ROWS 2 PRECEDING) AS 3-month moving sum FROM PropertySale WHERE branchNo = ‘B003’ AND yearMonth BETWEEN ('2004-01' AND '2004-06’) GROUP BY yearMonth ORDER BY yearMonth; 26
  • 27. + Windowing = 2 210000+350000+400000 = 960000 / 3 = 320000 27
  • 28. RATIO: Proportion of a value over the total set of values  calculates the ratio of an employee's salary over the sum of salaries in his department 2300+2500 = 4800 2300/4800 = 0,479166 28