Tablefunc
Table function utilities for cross tabulation and pivot operations
The 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
The crosstab
function transforms row-oriented data into a cross-tabulation format (pivot table).
Example: Sales by Quarter
Result:
Normal Random Numbers
The normal_rand
function generates normally distributed random numbers:
Hierarchical Queries
The connectby
function helps create hierarchical queries.
Let’s create a sample employee hierarchy and query it:
Result:
Additional Resources
Tablefunc is a powerful extension for working with tables and hierarchies. We only covered a few basic examples here, but there are many more options available. You can read the rest in PostgreSQL tablefunc Documentation