← All tasksSQL · Senior

Daily revenue and a running total

For each day with sales list day, revenue (the day's total) and running_total (the sum from the first day up to and including this one), ordered by date. A day can have several sales.

Tables

CREATE TABLE sales (
  id     int PRIMARY KEY,
  day    date NOT NULL,
  amount numeric(10, 2) NOT NULL
);

Tests (3)

Each test fills the tables with its own data and compares your query's result with the expected one: the same columns (names and order) and the same rows in the same order, so ORDER BY matters. The query runs in real PostgreSQL right in your browser; the first run downloads it (about 5 MB).

  1. INSERT INTO sales VALUES
      (1, '2024-03-01', 100.00),
      (2, '2024-03-01', 50.00),
      (3, '2024-03-02', 30.00),
      (4, '2024-03-04', 20.00);
  2. -- rows are not inserted in date order
    INSERT INTO sales VALUES
      (1, '2024-05-03', 5.00),
      (2, '2024-05-01', 10.00),
      (3, '2024-05-02', 1.50),
      (4, '2024-05-01', 2.50);
  3. INSERT INTO sales VALUES (1, '2024-01-01', 99.99);