tablefunc
extension provides a set of functions for manipulating tables, including cross tabulation, pivoting, and connecting tables.
Your Nile database arrives with the tablefunc extension already enabled.
Overview
The tablefunc extension provides several useful functions:crosstab
: Creates pivot tables and cross tabulationsnormal_rand
: Generates normally distributed random numbersconnectby
: Implements hierarchical queries
Cross Tabulation Functions
Basic Crosstab
Thecrosstab
function transforms row-oriented data into a cross-tabulation format (pivot table).
Example: Sales by Quarter
Normal Random Numbers
Thenormal_rand
function generates normally distributed random numbers:
Hierarchical Queries
Theconnectby
function helps create hierarchical queries.
Let’s create a sample employee hierarchy and query it: