PostgreSQL

Pivot With/Without Tablefunc

A Pivot Table is a powerful tool for estimating, compiling, and reviewing data to find patterns and trends even easier. Pivot Tables can be used to aggregate, sort, arrange, rearrange, group, total, or average data in a dataset to truly comprehend data associations and dependencies. Using a pivot table as an illustration is the easiest way to demonstrate how this method operates. PostgreSQL 8.3 was launched a few years back, and a new version named ‘tablefunc’ was added. Tablefunc is a component that contains several methods that yield tables (that is, multiple rows). This modification comes with a very cool range of features. The crosstab method, which will be used to create pivot tables, is among them. The crosstab method takes a textual argument: a SQL command that returns raw data in the first layout and returns a table in the subsequent layout.

Example Pivot Table Without TableFunc:

To start working on the PostgreSQL pivoting with the ‘tablefunc’ module, you have to try to make a pivot table without it. So let’s open the PostgreSQL command-line shell and provide the parameter values for the required server, database, port number, username, and password. Leave these parameters empty if you want to use the default selected parameters.

We will be creating a new table named ‘Test’ in the database ‘test’ with some fields in it, as displayed below.

>> CREATE TABLE Test(Id int, name varchar(20), sal int, job varchar(20));

After creating a table, it’s time to insert some values in the table, as shown from the below query.

>> INSERT INTO Test (Id, name, sal, job) VALUES (11, 'Aqsa', 45000, 'Writer'), (11, 'Aqsa', 48000, 'Officer'), (11, 'Aqsa', 50000, 'Doctor'), (12, 'Raza', 40000, 'Officer'), (11, 'Raza', 60000, 'Doctor'), (12, 'Raza', 67000, 'Officer'), (13, 'Saeed', 85000, 'Writer'), (13, 'Saeed', 69000, 'Officer'), (13, 'Saeed', 90000, 'Doctor');

You can see that the relevant data has been successfully inserted. You can see this table has more than 1 of the same values for id, name, and job.

>> SELECT * FROM input;

Let’s create a pivot table, which will summarize the record of the table ‘Test’ using the below query. The command merges the same values of the ‘Id’ and ‘name’ column in one row while taking the sum of ‘salary’ column values for the same data according to ‘Id’ and ‘name’. It’s also telling how many times one value has occurred in the particular set of values.

>> SELECT Id, name, sum(sal) sal, sum((job = 'Doctor')::int) Doctor, sum((job = 'Writer')::int) Writer, sum((job = 'Officer')::int) "Officer" FROM Test GROUP BY Id, name;

Example Pivot Table With TableFunc:

We’ll start by explaining our main point from a realistic standpoint, and then we’ll describe the pivot table creation in steps we like. So, first of all, you have to add three tables to work on a pivot. The first table we are going to create is ‘Makeup’, which will store information regarding makeup essentials. Try the below query in the command-line shell to create this table.

>> CREATE TABLE IF NOT EXISTS Makeup(make_Id int PRIMARY KEY, p_name VARCHAR(100) NOT NULL);

After the creation of table ‘Makeup’, let’s add some records to it. We will be executing the below-listed query in the shell to add 10 records to this table.

We need to create another table named ‘users’ which is going to hold the records of the users using those products. Execute the below-stated query in the shell to create this table.

>> CREATE TABLE IF NOT EXISTS users(user_id int PRIMARY KEY, u_name varchar(100) NOT NULL);

We have inserted the 20 records for the table ‘users’ as shown in the image below.

We have another table, ‘makeup_user’, which will hold the mutual records of both the ‘Makeup’ and ‘users’ table. It has another field, ‘price’, which will save the price of the product. The table has been generated by using the stated below query.

>> CREATE TABLE IF NOT EXISTS makeup_user( ID int PRIMARY KEY, Mid int NOT NULL REFERENCES Makeup(make_Id), Uid  int NOT NULL REFERENCES users(user_id), price decimal(18,2));

We have inserted a total of 56 records in this table, as shown in the picture.

Let’s create a view further to use it for the generation of a pivot table. This view is using INNER Join to match the primary key column values of all the three tables and fetch the ‘name’, ‘product_name’, and the ‘cost’ of a product from a table ‘customers’

>> CREATE VIEW v_makeup_users AS SELECT c.u_name, p.p_name, pc.price FROM users c INNER JOIN makeup_user pc ON c.user_id = pc.Uid INNER JOIN Makeup p ON pc.Mid = p.make_Id;

To use this, you must have to first install the tablefunc package for the database you want to use. This package is built-in PostgreSQL 9.1 and later released by running the below-stated command. The tablefunc package has been enabled for you now.

>> CREATE EXTENSION IF NOT EXISTS tablefunc;

After creating the extension, it’s time to use the Crosstab() function to create a pivot table. So we will be using the following query in the command-line shell to do so. This query is first fetching the record from the newly created ‘View’. These records will be ordered and grouped by the ascending order of columns ‘u_name’ and ‘p_name’. We have listed their makeup name for every customer, which they have purchased, and the total cost of products purchased in the table. We have applied the UNION ALL operator on the column ‘p_name’ to sum-up all the products purchased by one customer separately. This will sum-up all the costs of products purchased by a user into one value.

Our Pivot table has been ready and displayed in the image. You can clearly see that some column spaces are empty below every p_name because they have not purchased that particular product.

Conclusion:

We have now brilliantly learned how to create a pivot table to summarize the tables’ results with and without the use of the Tablefunc package.

About the author

Aqsa Yasin

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.