Pivoting Monthly Revenue Rows into Columns with Conditional Aggregation
CREATE TABLE dept_income (
dept_id INT,
mon VARCHAR(3),
income INT
);
INSERT INTO dept_income VALUES
(1,'Jan',8000),(1,'Feb',7000),(1,'Mar',6000),
(2,'Jan',9000),(2,'Feb',8000),(2,'Mar',7000);
The goal is to transform the above row-oriented data into a single row per department with twelve monthly columns.
Why GROUP BY ...
Posted on Fri, 25 Sep 2026 16:10:25 +0000 by ipwnzphp
SQL Aggregate Functions and Grouping Operations
Understanding Aggregate Functions
Aggregate functions enable statistical analysis across multiple rows in database tables. Common use cases include calculating total salaries, finding maximum values, or counting records that meet specific criteria.
Common Aggregate Functions
Functon
Purpose
MAX
Returns maximum value
MIN
Returns minimum ...
Posted on Sat, 13 Jun 2026 17:25:58 +0000 by limke