14. Odds 'n' Ends

Transposing a Result Set Using Oracle’s MODEL Clause

select max(d10) d10,
max(d20) d20,
max(d30) d30
from (
    select d10,d20,d30
    from ( select deptno, count(*) cnt from emp group by deptno )
    model
    dimension by(deptno d)
    measures(deptno, cnt d10, cnt d20, cnt d30)
    rules(
        d10[any] = case when deptno[cv()]=10 then d10[cv()] else 0 end,
        d20[any] = case when deptno[cv()]=20 then d20[cv()] else 0 end,
        d30[any] = case when deptno[cv()]=30 then d30[cv()] else 0 end
)
)

Extracting Elements of a String from Unfixed Locations

We have a string field that contains serialized log data. We want to parse through the string and extract the relevant information. Unfortunately, the relevant information is not at fixed points in the string.

Sample Input

We want to extract the values between the square brackets

Sample Output

Finding the Number of Days in a Year (an Alternate Solution)

We want to find the number of days in a year.

Searching for Mixed Alphanumeric Strings

We have a column with mixed alphanumeric data and want to return those rows that have both alphabetical and numeric characters.

Sample Output

Converting Whole Numbers to Binary

We want to convert a whole number to its binary representation.

Because of MODEL’s ability to iterate and provide array access to row values, it is a best choice for this operation.

Pivoting a Ranked Result Set

We want to rank the values in a table and then pivot the result set into three columns. The idea is to show the top three, the next three, and then all the rest.

Explanation

Adding a Column Header into a Double Pivoted Result Set

We want to stack two result sets and then pivot them into two columns. Additionally, we want to add a “header” for each group of rows in each column. For example, we have two tables containing information about employees working in different areas of development in your company

Sample Output

Parsing Serialized Data into Rows

We have serialized data (stored in strings) that you want to parse and return as rows. The values are delimited by colons, and a string may or may not have all three entries

Sample Output

Explanation

Calculating Percent Relative to Total

We want to report a set of numeric values, and you want to show each value as a percentage of the whole.

Sample Output

Last updated