Using PostgreSQL to Manage Business Inventory Data and Visualize It
Published: 2023-03-06 10:23:59

**Learn how to connect Postgres to DbVisualizer, import inventory dataset into Postgres, run SQL queries on Business Inventory data stored in a Postgres database using DbVisualizer to get important analytics and convert them to charts for visualization.**

The function of every business is to create products and sell them to customers. The goods produced make up the inventory of the business. When the business is small, ensuring that the goods produced are sold is not a headache.

However, keeping up with the inventory as the business grows might become daunting. To streamline the production process, you might need to employ inventory management.

In this article, you will learn how to use SQL to manage business inventory data held in a Postgres database to come up with meaningful analytics. You will then learn how to convert the analytics into charts for visualisation.

## What is Inventory Management?
Inventory management is the process of ordering, storing and selling a business’s inventory where inventory is one of the most valuable assets of a company. Employing inventory management will help the business minimise overstocking and improve the efficiency of the production process.

Some inventory management benefits include,

- Availability of supplies to keep production running.
- Preventing loss or theft of supplies and produced goods.
- Improving production and sales process.
- Dealing with market changes that affect the demand and supply of goods.

## Prerequisites
To follow through with this tutorial, you need a database, an inventory dataset and a SQL client. In this case, we will use Postgres as the database, the Kaggle inventory dataset, and DbVisualizer as the database SQL client.

First, we will connect Postgres to DbVisualizer. Next, we will import the inventory dataset into Postgres using DbVisualizer. Then we will run SQL queries using DbVisualizer on the dataset to come up with important analytics. Finally, we will use DbVisualizer to create charts from the dataset.

## What is Postgres and DbVisualizer?
PostgreSQL, also known as Postgres, is a free and open-source relational database management system. Postgres is available for Windows, macOS and Linux.

DbVisualizer is a SQL client or a database management and analysis tool for all major databases. The SQL client provides SQL tools such as SQL editor with support for auto-completion, parameterized SQLs, SQL formatter, visual query builder, command-line based interface and more.

## Installing Postgres
To install Postgres, navigate to the Postgres download page and download it for your operating system. You can follow this guide to install Postgres on Windows, this guide to install on Linux and this guide to install on macOS.

## Installing DbVisualizer
To install DbVisualizer, navigate to the [DbVisualizer download](https://www.dbvis.com/download/) page. Then download the recommended installer for your operating system.

After that, execute the installer you have downloaded and follow the instructions that follow. When you start DbVisualizer, you are prompted to choose the DbVisualizer plan to proceed. You can proceed with the DbVisualizer free version with limited features or get a a 21-day free trial.

## Connecting Postgres To DbVisualizer
**Step 1:** Open Postgres using the command line or SQL shell as shown below.
<br \>
![Open Postgres](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kwsdofj8x6gtvmfuzmx4.png)
<figure><figcaption>Open Postgres</figcaption></figure>
<br \>
**Step 2:** Create a database called `inventory` in Postgres by running the command below on the command line.

```
$   create database inventory;
```
You can check if the database has been created by typing forward slash together with L and hitting enter. After that, you should see the inventory database we created on the list of databases in Postgres, as shown below.
<br \>
![List of databases in Postgres](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6hvysjosdgcszq9wz2m.png)
<figure><figcaption>List of databases in Postgres</figcaption></figure>
<br \>
**Step 3:** Start DbVisualizer and click `create a connection` button as shown below.
<br \>
![Create a connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vcoy78gr82iv9icch2oa.png)
<figure><figcaption>Create a connection</figcaption></figure>
<br \>
**Step 4:** Search and select Postgres driver from the popup menu on the left side of your screen, as shown below.
<br \>
![Search and select driver](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4sf8sm5on0kl6jwnaod5.png)
<figure><figcaption>Search and select driver</figcaption></figure>
<br \>
An object view tab for the Postgres connection is opened.

**Step 5:** Fill in the empty fields as shown below

**Name:** InventoryManagement
**Database:** inventory
**Database Userid:** postgres
**Database Password:** Enter Your Postgres password

Once you have filled all the empty fields, click the connect button at the bottom. If the connection is succesful, your screen should look as shown below.
<br \>
![Connect to Postgres](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nc81fj8p94nouxtnm4qr.png)
<figure><figcaption>Connect to Postgres</figcaption></figure>
<br \>

## Importing Data Table Into Postgres Using DbVisualizer
**Step 1:** Navigate to this [Kaggle web page](https://www.kaggle.com/code/danavg/abc-analysis-of-active-inventory/data) and download the inventory dataset.
<br \>
![Download inventory dataset](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ussrs3eo92iwbivgqcb.png)
<figure><figcaption>Download inventory dataset</figcaption></figure>
<br \>
**Step 2:** Next, Open the database connection `Inventory Management` tab tree as shown below.
<br \>
![Inventory management connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2gtp25fx4zi61n84r7m.png)
<figure><figcaption>Inventory management connection</figcaption></figure>
<br \>
Inside the connection tab tree, open the Databases tab tree.
<br \>
![Databases tab tree](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kz2o79yj9xqoesnhz8wj.png)
<figure><figcaption>Open the databases tab tree</figcaption></figure>
<br \>
Inside the Databases tab tree, open the inventory database tab tree.
<br \>
![Open the inventory db tab tree](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5jdwnznobnahm1s59wb.png)
<figure><figcaption>Open the inventory db tab tree</figcaption></figure>
<br \>
Inside the inventory database tab tree, open the schemas tab tree.
<br \>
![Schema tab tree](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/feoy8fp2sjmkt1x2r0sl.png)
<figure><figcaption>Schema tab tree</figcaption></figure>
<br \>
Inside the schemas tab tree, open the public tab tree.
<br \>
![Open the public tab tree](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pzlpqt8fjntgie9b00g6.png)
<figure><figcaption>Open the public tab tree</figcaption></figure>
<br \>
**Step 3:** Right-click on the Tables tab tree and select Import Table Data.
<br \>
![Select Import Table Data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/srxl8ff1drtnyv7n2oyg.png)
<figure><figcaption>Select Import Table Data</figcaption></figure>
<br \>
**Step 4:** When you reach the popup window, navigate to the directory you downloaded the inventory dataset CSV file into and click the Open button.
<br \>
![Navigate to the directory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7j7pxk0sv9815gfc73nj.png)
<figure><figcaption>Navigate to directory</figcaption></figure>
<br \>
**Step 5:** Keep clicking the Next button below the popup window until you reach the window shown below.
<br \>
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nq70gtfkhj2l23h6hj81.png)
<br \>
The first thing to do on this popup window is to give the table that will hold the inventory dataset a name. In this case, I have named my table `InventoryData` as shown below.
<br \>
![Inventory data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2th4eh6bqk03180j8v3d.png)
<br \>
The second thing to do is change all `bit data types` to `boolean data types` by clicking the data type as shown below. This is because the database table accepts boolean data types instead of bit data types.
<br \>
![Change data type](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jw02ofihk90tda6ha6qd.png)
<figure><figcaption>Change data type</figcaption></figure>
<br \>
Next check the Use `Delimited Identifiers` checkbox as shown below. This is because in our dataset we have a column called Order. Order is a reserved word in Postgres and checking `Use Delimited Identifiers` will help Postgres to differentiate column name order from the reserved name Order.
<br \>
![Use delimited identifiers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yauarwi38xwv098bhuwf.png)
<figure><figcaption>Use Delimited Identifiers</figcaption></figure>
<br \>
Click next and then import the dataset into Postgres. Once the dataset is imported, right-click on the table tab and refresh to see the table data as shown below.
<br \>
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g9tcopa4wzg63gr3lhja.png)
<br \>
Open the table and navigate to the Data tab. You should be able to see the dataset imported, as shown below.
<br \>
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wcssmrke8enjuaytbjo7.png)
<br \>

## Running SQL Queries
The dataset contains historical sales data identified with the column titled `File_Type` together with current active inventory that needs evaluation. The historical data shows sales for the past 6 months.

The `SoldFlag` column shows products that have been sold in the last 6 months as true and those that have not sold in that period as false. The `SKU_number` column has the unique identifier for each product, while the `PriceReg` column has the price of each product.

As a data analyst, you can come up with stats from the dataset, such as the following:

- Sales revenue each product made in the last 6 months.
- Products not sold in the last 6 months.
- Ten best selling products in the last 6 months.

## Sales revenue each product made in the last 6 months.
To get sales revenue each product made in the last months, create a SQL query that multiplies the `PriceReg` column with the `ItemCount` column where the `SoldFlag` column is true.

The SQL query you can create to get the `TotalSales` of the products sold in the last 6 months should look as shown below.

```
SELECT "SKU_number", "PriceReg" * "ItemCount" AS “TotalSales”
FROM inventorydata WHERE "SoldFlag"= 'True';
```
To run the SQL query on DbVisualizer, click the `SQL Commander` tab at the top of your screen and select `New SQL Commander` as shown below.
<br \>
![Click SQL Commander tab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7cgl3m00543vj9glyxx0.png)
<figure><figcaption>Click SQL Commander tab</figcaption></figure>
<br \>
Once the SQL Commander opens, type the SQL query and press the (Ctrl+Enter) keys to run the query. You should now see a new table with columns SKU_number and TotalSales, as shown below.
<br \>
![View of SKU_number and TotalSales](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/isvvjjwadljgl1a48bs0.png)
<br \>

## Products not sold in the last 6 months?
To get the number of products that were not sold in the last 6 months, create a SQL query that counts the `SKU_number` column where the `SoldFlag` column is false.

The SQL query should look as shown below.

```
SELECT COUNT("SKU_number") FROM inventorydata WHERE "SoldFlag"='False';
```
Run the SQL query on DbVisualizer using `(Ctrl+Enter)` keys, and you should see that `63000 products` were not sold in the last 6 months as shown below.
<br \>
![Number of products not sold](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kk80392re495abi0ferp.png)
<br \>

## Ten best selling products in the last 6 months.
To get the top 10 selling products in the last 6 months, create a SQL query that finds products' total sales in the last 6 months. Next, build a SQL query that groups the total sales by columns `SKU_number` and `TotalSales`.

After that, the SQL query should order the sales by the `TotalSales` column, where they are limited to only the first 10 products in a descending order. The SQL query should look as shown below.

```
SELECT "SKU_number", "PriceReg" * "ItemCount" AS "TotalSales"
FROM inventorydata WHERE "SoldFlag"= 'True'
GROUP BY "SKU_number", "TotalSales"
ORDER BY "TotalSales"
DESC LIMIT 10;
```
Run the SQL query on DbVisualizer using `(Ctrl+Enter)` keys and you should see a table of top 10 selling products in the last 6 months as shown below.
<br \>
![Products sold](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hncqg7u1fqnucdsyp25h.png)
<br \>

## Visualizing Data Using Charts on DbVisualizer
On DbVisualizer, you can present data from any table in a configurable chart displayed in a line, bar, area or pie chart. The charting support is similar to that of MsExcel. The charts can be exported as an image to file, printed and copied to the system clipboard for easy sharing.

To activate the chart view, click the rightmost button in the result toolbar as shown below.
<br \>
![Activate chart view](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bdfh8uf8nbsrw80s3pp6.png)
<br \>
When switching to the chart view, DbVisualizer automatically picks the first date or text column as the X-axis for the chart and the first numeric column as the Y-axis. In the following example, the SKU_number is the X-axis and TotalSales as Y-axis as shown below.
<br \>
![Chart view](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3z2elquu6jmklh57auix.png)
<br \>

## Visualizing Top 10 Selling Products
First, Activate the chart view on the best selling products.
<br \>
![chart view](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yc6yvjuy5evgx2cycc1l.png)
<br \>
Once that’s done, you should see a chart where SKU_number is on X-axis and TotalSales on Y-axis as shown below.
<br \>
![Cbart](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tj7gdciu3xhcflvx3rc9.png)
<br \>
The chart shows that the highest sold product has made over 200,000 in revenue compared to the second highest selling product that has made less than 125,000 in revenue.

## Conclusion
In this article, we have walked you through PostgreSQL and DbVisualizer while helping you to work with a data set related to the inventory of a business. Doing so can help you to come up with exciting insights that can help the business management to make the right decisions that can lead to its growth.

I challenge you to create different SQL queries and see what insights you can come up with using [DbVisualizer - come back to our blog to learn what’s new on the database front after a while](https://www.dbvis.com/thetable/?_ga=2.193739438.2031003048.1677487719-628703149.1635148413&_gl=1*12jwlsf*_ga*NjI4NzAzMTQ5LjE2MzUxNDg0MTM.*_ga_PTZV59MQGC*MTY3NzY4Nzg5Ny40ODkuMC4xNjc3Njg3ODk3LjYwLjAuMA..), and until next time!

## About the author
Bonnie is a web developer and technical writer creating easy-to-understand technical articles.