12. Reporting and Reshaping

Pivoting a Result Set into One Row

We want to take values from groups of rows and turn those values into columns in a single row per group.

Sample Input

Sample Output

Pivoting a Result Set into Multiple Rows

We want to turn rows into columns by creating a column corresponding to each of the values in a single given column.

Sample Input

Sample Output

Explanation

Step 1:

Step 2:

Note the blank spaces are null value. To remove the NULLs, use the aggregate function MAX or MIN and group by RN.

Reverse Pivoting a Result Set

We want to transform columns to rows.

Sample Input

Sample Output

Explanation

There are three columns, and we need to create three rows. Begin by creating a Cartesian product between inline view EMP_CNTS and some table expression that has at least three rows. Following sample code uses table DEPT to create the Cartesian product. The Cartesian product enables you to return a row for each column in inline view EMP_CNTS.

Reverse Pivoting a Result Set into One Column

We want to return all columns from a query as just one column

Sample Output

Use the window function ROW_NUMBER OVER to rank each row based on EMPNO (1–4). Then use a CASE expression to transform three columns into one.

Explanation

Step 1: Use the window function ROW_NUMBER OVER to create a ranking for each employee in DEPTNO 10

Step 2: Add the Cartesian product

Step 3: Use a CASE expression to put ENAME, JOB, and SAL into one column for each employee

Suppressing Repeating Values from a Result Set

We are generating a report, and when two rows have the same value in a column, you want to display that value only once. For example below sample output.

Pivoting a Result Set to Facilitate Inter-Row Calculations

We want to make calculations involving data from multiple rows. For that, we want to pivot those rows into columns such that all values we need are then in a single row.

Creating Buckets of Data, of a Fixed Size

We want to organize data into evenly sized buckets, with a predetermined number of elements in each bucket. The total number of buckets may be unknown, but we want to ensure that each bucket has five elements.

Sample Output

Creating a Predefined Number of Buckets

We want to organize the data into a fixed number of buckets. For example, we want to organize the employees in table EMP into four buckets.

Sample Output

NTILE organizes an ordered set into the number of buckets we specify, with any stragglers distributed into the available buckets starting from the first bucket

We want to use SQL to generate histograms that extend horizontally. For example, see below output.

Creating Vertical Histograms

We want to generate a histogram that grows from the bottom up. For example, below output.

Returning Non-GROUP BY Columns

We are executing a GROUP BY query, and want to return columns in the select list that are not also listed in GROUP BY clause. This is not usually possible, as such ungrouped columns would not represent a single value per row.

We want to find the employees who earn the highest and lowest salaries in each department, as well as the employees who earn the highest and lowest salaries in each job. Also, want to see each employee’s name, the department he works in, his job title, and his salary. Sample output below.

Calculating Simple Subtotals

An example would be a result set that sums the salaries in table EMP by JOB and that also includes the sum of all salaries in table EMP.

A simple subtotal is defined as a result set that contains values from the aggregation of one column along with a grand total value for the table.

Calculating Subtotals for All Possible Expression Combinations

We want to find the sum of all salaries by DEPTNO, and by JOB, for every JOB/ DEPTNO combination.

Sample Output

Identifying Rows That Are Not Subtotals

We have used the CUBE extension of the GROUP BY clause to create a report, and need a way to differentiate between rows that would be generated by a normal GROUP BY clause and those rows that have been generated as a result of using CUBE or ROLLUP.

Sample Output

Using Case Expressions to Flag Rows

We want to return the following result set.

Creating a Sparse Matrix

We want to create a sparse matrix, such as the following one transposing the DEPTNO and JOB columns of table EMP.

Grouping Rows by Units of Time

We want to summarize data by some interval of time. For example, we have a transaction log and want to summarize transactions by five-second intervals.

Sample Input

Sample Output

Performing Aggregations over Different Groups/ Partitions Simultaneously

We want to aggregate over different dimensions at the same time. For example, return a result set that lists each employee’s name, their department, the number of employees in their department (themselves included), the number of employees that have the same job (themselves included in this count as well), and the total number of employees in the EMP table.

Performing Aggregations over a Moving Range of Values

We want to compute a moving aggregation, such as a moving sum on the salaries in table EMP. We want to compute a sum for every 90 days, starting with the HIREDATE of the first employee.

Pivoting a Result Set with Subtotals

We want to create a report containing subtotals and then transpose the results to provide a more readable report.

Sample Output

Last updated