SQL CUBE

SQL CUBE

 

SQL CUBE



Summary: in this tutorial, you will learn how to use the SQL CUBE to generate subtotals for the output of a query.

Introduction to SQL CUBE

Similar to the ROLLUPCUBE is an extension of the GROUP BY clause. CUBE allows you to generate subtotals like the ROLLUP extension. In addition, the CUBE extension will generate subtotals for all combinations of grouping columns specified in the GROUP BY clause.

The following illustrates the syntax of CUBE extension:

SELECT c1, c2, AGGREGATE_FUNCTION(c3) FROM table_name GROUP BY CUBE(c1 , c2);

In this syntax, we have two columns specified in the CUBE. The statement creates two subtotal combinations. Generally, if you have n number of columns listed in the CUBE, the statement will create 2n subtotal combinations.

SQL CUBE examples

We will reuse the inventory table created in the ROLLUP tutorial.

SQL CUBE with one column example

The following statement uses the SUM() function and the GROUP BY clause to find the total inventory of every warehouse:

SELECT warehouse, SUM(quantity) FROM inventory GROUP BY warehouse;

If you want to know the total inventory in all warehouses, you use the CUBE extension in the GROUP BY clause as follows:

SELECT warehouse, SUM(quantity) FROM inventory GROUP BY CUBE(warehouse) ORDER BY warehouse;

In this example, the CUBE the extension adds a total inventory row with a null value in the warehouse column. The effect is the same as the ROLLUP function. To make the output more readable, you can use the COALESCE() function as shown below:

SELECT COALESCE(warehouse,'All warehouses'), SUM(quantity) FROM inventory GROUP BY CUBE(warehouse) ORDER BY warehouse;

SQL CUBE with multiple columns example

The following statement finds the total inventory by warehouse and product:

SELECT warehouse, product, SUM(quantity) FROM inventory GROUP BY warehouse,product ORDER BY warehouse, product;

When you use the CUBE function, the query makes four subtotals:

SELECT warehouse, product, SUM(quantity) FROM inventory GROUP BY CUBE(warehouse,product) ORDER BY warehouse, product

As you can see in the output, we have four subtotal rows:

  • The third and sixth rows show the total inventory of all products in the San Francisco and San Jose warehouses. The values in the product the column is null.
  • The seventh and eighth rows display the total inventory by-products which are Samsung and iPhone in all warehouses. Hence, the values in the warehouse columns are null.

The last column is the grand total that shows the total inventory in all warehouses.

The following statement uses the COALESCE() function to substitute null values with more meaningful data:

SELECT COALESCE(warehouse, '...All Warehouses') warehouse, COALESCE(product, '...All Products') product, SUM(quantity) FROM inventory GROUP BY CUBE(warehouse,product) ORDER BY warehouse, product;

Creating cross-tabular reports

The following query creates a cross-tabular report by retrieving data from the  employees the table in the sample database using the CUBE extension:

SELECT COALESCE(department_name, '-') department, COALESCE(job_title,'-') job, COUNT(*) , SUM(salary) salary FROM employees INNER JOIN departments USING (department_id) INNER JOIN jobs USING (job_id) GROUP BY CUBE(department_name,job_title) ORDER BY department_name ASC NULLS LAST;

Here is the result:

DepartmentJobCOUNT(*)SALARY
AccountingAccounting Manager112000
AccountingPublic Accountant18300
Accounting220300
AdministrationAdministration Assistant14400
Administration14400
ExecutiveAdministration Vice President234000
ExecutivePresident124000
Executive358000
FinanceAccountant539600
FinanceFinance Manager112000
Finance651600
Human ResourcesHuman Resources Representative16500
Human Resources16500
ITProgrammer528800
IT528800
MarketingMarketing Manager113000
MarketingMarketing Representative16000
Marketing219000
Public RelationsPublic Relations Representative110000
Public Relations110000
PurchasingPurchasing Clerk513900
PurchasingPurchasing Manager111000
Purchasing624900
SalesSales Manager227500
SalesSales Representative324000
Sales551500
ShippingShipping Clerk27900
ShippingStock Clerk12700
ShippingStock Manager430600
Shipping741200
Accountant539600
Accounting Manager112000
Administration Assistant14400
Administration Vice President234000
Finance Manager112000
Human Resources Representative16500
Marketing Manager113000
Marketing Representative16000
President124000
Programmer528800
Public Accountant18300
Public Relations Representative110000
Purchasing Clerk513900
Purchasing Manager111000
Sales Manager227500
Sales Representative324000
Shipping Clerk27900
Stock Clerk12700
Stock Manager430600
39316200

In this tutorial, you have learned how to use the SQL CUBE extensions to generate the subtotal in the output of a query.

Reactions

Post a Comment

0 Comments

close