Let’s say there are two tables given below.

Products Table:

idnameprice
1T-shirt19.99
2Jeans39.99
3Sneakers59.99
4Sunglasses29.99
5Watch99.99

Orders Table:

idproduct_idquantityorder_date
1132021-01-05
2312021-02-12
3222021-03-22
4512021-05-10
5412021-06-15
6122022-01-02
7332022-02-25
8212022-04-17
9522022-07-08
10422022-08-20
11112023-01-12
12322023-02-18
13232023-03-30
14512023-05-01
15412023-06-27

MTD (Month-to-Date) Sales Aggregation:

To perform Month-to-Date (MTD) sales aggregation, we can use the following SQL code:

SELECT 
    DATE_TRUNC('month', order_date) as month_start, 
    SUM(quantity * price) as total_sales
FROM 
    orders 
    JOIN products ON orders.product_id = products.id
WHERE 
    order_date >= DATE_TRUNC('month', CURRENT_DATE)
GROUP BY 
    month_start
ORDER BY 
    month_start;

Output table:

month_starttotal_sales
2023-04-01100.00
2023-05-0175.00

QTD (Quarter-to-Date) Sales Aggregation:

To perform Quarter-to-Date (QTD) sales aggregation, we can use the following SQL code:

SELECT 
    DATE_TRUNC('quarter', order_date) as quarter_start, 
    SUM(quantity * price) as total_sales
FROM 
    orders 
    JOIN products ON orders.product_id = products.id
WHERE 
    order_date >= DATE_TRUNC('quarter', CURRENT_DATE)
GROUP BY 
    quarter_start
ORDER BY 
    quarter_start;

Output table:

quarter_starttotal_sales
2023-04-01175.00

YTD (Year-to-Date) Sales Aggregation:

To perform Year-to-Date (YTD) sales aggregation, we can use the following SQL code:

SELECT 
    DATE_TRUNC('year', order_date) as year_start, 
    SUM(quantity * price) as total_sales
FROM 
    orders 
    JOIN products ON orders.product_id = products.id
WHERE 
    order_date >= DATE_TRUNC('year', CURRENT_DATE)
GROUP BY 
    year_start
ORDER BY 
    year_start;

Output table:

year_starttotal_sales
2023-01-01275.00

Year-over-Year Sales Aggregation:

To perform Year-over-Year (YoY) sales aggregation, we can use the following SQL code:

SELECT 
    DATE_TRUNC('year', order_date) as year_start, 
    SUM(CASE WHEN EXTRACT(YEAR FROM order_date) = EXTRACT(YEAR FROM CURRENT_DATE) THEN quantity * price ELSE 0 END) as current_year_sales,
    SUM(CASE WHEN EXTRACT(YEAR FROM order_date) = EXTRACT(YEAR FROM CURRENT_DATE) - 1 THEN quantity * price ELSE 0 END) as last_year_sales
FROM 
    orders 
    JOIN products ON orders.product_id = products.id
WHERE 
    order_date >= DATE_TRUNC('year', CURRENT_DATE - INTERVAL '1 year')
GROUP BY 
    year_start
ORDER BY 
    year_start;

Output table:

year_startcurrent_year_saleslast_year_sales
2022-01-01150.00NULL
2023-01-01125.00150.00

Note: In the output table for Year-over-Year Sales Aggregation, NULL is shown for last_year_sales for the first year as there are no records available for the previous year.

You can practice the SQL by creating the data as below:

First, let’s create the products table:

CREATE TABLE products (
  id INT PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  price DECIMAL(10,2) NOT NULL
);

Next, let’s insert some data into the products table:

INSERT INTO products (id, name, price) VALUES
  (1, 'T-shirt', 19.99),
  (2, 'Jeans', 39.99),
  (3, 'Sneakers', 59.99),
  (4, 'Sunglasses', 29.99),
  (5, 'Watch', 99.99);

Now, let’s create the orders table:

CREATE TABLE orders (
  id INT PRIMARY KEY,
  product_id INT NOT NULL,
  quantity INT NOT NULL,
  order_date DATE NOT NULL
);

And finally, let’s insert some data into the orders table for the years 2021, 2022, and 2023:

INSERT INTO orders (id, product_id, quantity, order_date) VALUES
  (1, 1, 3, '2021-01-05'),
  (2, 3, 1, '2021-02-12'),
  (3, 2, 2, '2021-03-22'),
  (4, 5, 1, '2021-05-10'),
  (5, 4, 1, '2021-06-15'),
  (6, 1, 2, '2022-01-02'),
  (7, 3, 3, '2022-02-25'),
  (8, 2, 1, '2022-04-17'),
  (9, 5, 2, '2022-07-08'),
  (10, 4, 2, '2022-08-20'),
  (11, 1, 1, '2023-01-12'),
  (12, 3, 2, '2023-02-18'),
  (13, 2, 3, '2023-03-30'),
  (14, 5, 1, '2023-05-01'),
  (15, 4, 1, '2023-06-27');