1 May 2024

Pivoting in SQL

 Pivoting in SQL involves transforming row-level data into columnar data. This is particularly useful when you want to rotate data from multiple rows into a single row with multiple columns. Here's an example to illustrate pivoting in SQL:

Let's say you have a table Sales with the following structure:

mathematica
| CustomerID | Product | Amount | |------------|-----------|--------| | 1 | Product A | 100 | | 1 | Product B | 200 | | 2 | Product A | 150 | | 3 | Product B | 300 |

You want to pivot this data to show the total sales amount for each product by customer, like this:

sql
| CustomerID | Product A | Product B | |------------|-----------|-----------| | 1 | 100 | 200 | | 2 | 150 | NULL | | 3 | NULL | 300 |

You can achieve this using SQL's PIVOT syntax. Here's how you can do it:

sql
SELECT CustomerID, [Product A], [Product B] FROM (SELECT CustomerID, Product, Amount FROM Sales) AS SourceTable PIVOT ( SUM(Amount) FOR Product IN ([Product A], [Product B]) ) AS PivotTable;

Explanation:

  • The inner query (SELECT CustomerID, Product, Amount FROM Sales) AS SourceTable selects the necessary columns from the Sales table.
  • The PIVOT keyword is used to pivot the data.
  • SUM(Amount) aggregates the amounts for each product and customer combination.
  • FOR Product IN ([Product A], [Product B]) specifies the columns that you want to pivot.
  • AS PivotTable is an alias for the pivoted result set.

The result will be the pivoted table with CustomerID, Product A, and Product B columns showing the total sales amount for each product by customer.

23 Apr 2024

SQL Partition

 SQL partitioning is a technique used to divide large tables into smaller, more manageable parts. It can improve query performance, data management, and maintenance tasks. Partitioning involves splitting a table or index into multiple segments, called partitions, based on certain criteria, such as ranges of values or specific column values. Each partition can be stored separately, allowing for faster access to data relevant to a query.

Here's a simple example to illustrate SQL partitioning:

Let's say we have a table named sales containing sales data with columns sale_date, product_id, quantity_sold, and revenue.

sql
CREATE TABLE sales ( sale_id INT, sale_date DATE, product_id INT, quantity_sold INT, revenue DECIMAL(10, 2) );

Now, let's partition this table by sale_date into monthly partitions. We'll create a partition for each month.

sql
CREATE TABLE sales_partitioned ( sale_id INT, sale_date DATE, product_id INT, quantity_sold INT, revenue DECIMAL(10, 2) ) PARTITION BY RANGE (YEAR(sale_date), MONTH(sale_date));

This CREATE TABLE statement specifies that the sales_partitioned table will be partitioned by the year and month of the sale_date column.

Next, we define individual partitions for each month. For example, to create a partition for January 2024:

sql
CREATE TABLE sales_partitioned_2024_01 PARTITION OF sales_partitioned FOR VALUES FROM ('2024-01-01') TO ('2024-02-01');

Similarly, we create partitions for each subsequent month.

When you insert data into the sales_partitioned table, the database system automatically places the data into the appropriate partition based on the sale_date.

sql
INSERT INTO sales_partitioned (sale_id, sale_date, product_id, quantity_sold, revenue) VALUES (1, '2024-01-15', 1001, 10, 150.00), (2, '2024-02-20', 1002, 15, 200.00), ... ;

By partitioning the sales table, queries that involve filtering by sale_date can be optimized. For example, if you run a query to retrieve sales data for January 2024, the database engine will only scan the January partition, which contains relevant data, rather than scanning the entire table. This can significantly improve query performance, especially for large datasets.

Union Budget 2024-25 – All You Need to Know

  Union Budget 2024-25 – All You Need to Know The Union Budget is a yearly financial plan presented by the Finance Minister of India for the...