Posts

Showing posts with the label sql

PostgreSQL Practice Series

Image
Introduction PostgreSQL is a powerful open-source relational database management system, developed with many powerful features, high performance and famous for its reliability applied across various diverse projects. The outstanding advantages include: High data integrity and full ACID compliance. Powerful extensibility, allowing users to define their own data types, functions and custom indexes. Support for a wide variety of data types from structured (SQL) to unstructured (JSON/JSONB, XML). A strong development community, continuously updated and optimally secure. Detail Installing PostgreSQL with Docker Explanation of PostgreSQL Operations TOAST Storage Strategies Common PostgreSQL Statements Data Type Numeric Types Casting Design Schema Normalization and Denormalization Data Integrity OLTP and OLAP Table relationships Aggregate Constraint Index B-Tree B-Tree Index B-Tree Index with order Using Hash Index GIN Index GIN Index with Array GIN Index with TEXT GIN Index with JsonB GiST I...

Pivot in PostgreSQL

Image
Introduction Pivot (or Pivot Table / Crosstab) is a technique used to transform data from rows to columns. This technique is very popular in data analysis and reporting, helping group scattered data rows into an easy-to-read summary table. Example of initial data (Long format): | Year | Quarter | Revenue | | : --- | : ------ | : ------ | | 2023 | Q1 | 100 | | 2023 | Q2 | 150 | | 2024 | Q1 | 120 | | 2024 | Q2 | 180 | Data after Pivot (Wide format): | Year | Q1 | Q2 | | : --- | : --- | : --- | | 2023 | 100 | 150 | | 2024 | 120 | 180 | How to Implement Unlike SQL Server or Oracle (which have native PIVOT syntax), PostgreSQL does not have a direct PIVOT keyword. You can accomplish this using the following common approaches: Using conditional aggregate functions ( SUM + FILTER or CASE ) is the simplest method, requiring no additional extensions and optimizing performance well. Using the tablefunc extension and the crosstab() fun...

Aggregate Functions

Image
Introduction Aggregate Functions are functions that perform a calculation on a set of values (multiple rows) and return a single value. They are often used with the GROUP BY clause to group data, or combined with OVER to become Window Functions to calculate without losing detailed rows. Postgres natively supports Aggregate Functions which can be divided into the following groups: Basic Aggregate Functions SUM(column) : Calculates the sum of all numeric values in the column (ignores NULL). AVG(column) : Calculates the average value of the column (ignores NULL). COUNT : Counts the number of rows COUNT(*) counts all rows, including rows with NULL values COUNT(column) only counts rows with non-NULL values. MAX(column) : Finds the maximum value in the column, works with numbers, strings and dates. MIN(column) : Finds the minimum value in the column. NULL Handling Functions COALESCE(val1, val2, ..., valN) : Returns the first non- NULL value in the input list. This function is often pair...