AI Hints
Home›PostgreSQL Tutorial›Project: Harbor Events, Part 2
PostgreSQL · Lesson 109 of 120

Project: Reports

Money by venue and month.

⏱ 2 min read▶ Video at 1:38:57🎓 Free
Watch this lesson in the PostgreSQL courseWatch on YouTube ↗Full PostgreSQL course

At the end of each month, Harbor's owner wants revenue for each venue, and each venue's share of that month's total.

  • Revenue
  • Per venue
  • Share of month

Monthly revenue and share

The CTE sums seats times price, by month and venue. The window function adds up each month's total across venues, so we can work out each share. Run it. January and February, venue by venue.

query.sqlsql
WITH sales AS (
  SELECT date_trunc('month', e.starts_at)::date AS month, v.name AS venue,
         sum(b.seats * e.price) AS revenue
  FROM bookings b JOIN events e USING (event_id) JOIN venues v USING (venue_id)
  WHERE e.starts_at < '2026-03-01'
  GROUP BY 1, 2)
SELECT month, venue, revenue,
       round(100 * revenue / sum(revenue) OVER (PARTITION BY month), 1) AS pct
FROM sales ORDER BY month, revenue DESC;
Output
   month    |      venue      | revenue | pct
------------+-----------------+---------+------
 2026-01-01 | Pier Theatre    | 4680.00 | 38.6
 2026-01-01 | Lighthouse Loft | 4405.00 | 36.4
 2026-01-01 | Harbor Hall     | 3030.00 | 25.0
 2026-02-01 | Harbor Hall     | 4420.00 | 36.6
 2026-02-01 | Lighthouse Loft | 3895.00 | 32.2
 2026-02-01 | Pier Theatre    | 3775.00 | 31.2
(6 rows)
Also uses _setup.sql
CREATE TABLE venues (
  venue_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  name     text NOT NULL UNIQUE,
  city     text NOT NULL,
  capacity integer NOT NULL CHECK (capacity > 0));
CREATE TABLE events (
  event_id  integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  venue_id  integer NOT NULL REFERENCES venues,
  title     text NOT NULL,
  starts_at timestamptz NOT NULL,
  price     numeric(6,2) NOT NULL CHECK (price >= 0));
CREATE TABLE bookings (
  booking_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  event_id   integer NOT NULL REFERENCES events,
  email      text NOT NULL,
  seats      integer NOT NULL CHECK (seats BETWEEN 1 AND 10),
  booked_at  timestamptz NOT NULL DEFAULT now(),
  UNIQUE (event_id, email));
INSERT INTO venues (name, city, capacity) VALUES
  ('Harbor Hall', 'Halifax', 300), ('Pier Theatre', 'Halifax', 120),
  ('Lighthouse Loft', 'Lunenburg', 60);

INSERT INTO events (venue_id, title, starts_at, price)
SELECT 1 + g % 3,
       (ARRAY['Jazz Night', 'Comedy Hour', 'Folk Session', 'Poetry Slam',
              'Sea Shanty Evening', 'Indie Film Night'])[1 + g % 6],
       timestamptz '2026-01-02 19:30' + g * interval '2 days',
       (ARRAY[15, 20, 25, 30])[1 + g % 4]
FROM generate_series(1, 60) g;

SELECT setseed(0.7);
INSERT INTO bookings (event_id, email, seats, booked_at)
SELECT e, 'guest' || c || '@mail.com', 1 + (random() * 2)::int,
       timestamptz '2026-01-01' + random() * interval '100 days'
FROM generate_series(1, 60) e, generate_series(1, 40) c
WHERE random() < 0.5;
Also uses _empty
1

Fill rate by show type

Which kinds of show fill the room? Seats sold divided by capacity gives a fill rate per event, then we average by title. Run it. The show types, from fullest to emptiest.

query.sqlsql
WITH per_event AS (
  SELECT e.title, sum(b.seats)::numeric / v.capacity AS fill
  FROM events e JOIN venues v USING (venue_id) JOIN bookings b USING (event_id)
  GROUP BY e.event_id, e.title, v.capacity)
SELECT title, round(100 * avg(fill)) AS fill_pct
FROM per_event GROUP BY title ORDER BY fill_pct DESC;
Output
       title        | fill_pct
--------------------+----------
 Indie Film Night   |       70
 Folk Session       |       66
 Sea Shanty Evening |       35
 Comedy Hour        |       33
 Poetry Slam        |       13
 Jazz Night         |       13
(6 rows)
Also uses _setup.sql
CREATE TABLE venues (
  venue_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  name     text NOT NULL UNIQUE,
  city     text NOT NULL,
  capacity integer NOT NULL CHECK (capacity > 0));
CREATE TABLE events (
  event_id  integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  venue_id  integer NOT NULL REFERENCES venues,
  title     text NOT NULL,
  starts_at timestamptz NOT NULL,
  price     numeric(6,2) NOT NULL CHECK (price >= 0));
CREATE TABLE bookings (
  booking_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  event_id   integer NOT NULL REFERENCES events,
  email      text NOT NULL,
  seats      integer NOT NULL CHECK (seats BETWEEN 1 AND 10),
  booked_at  timestamptz NOT NULL DEFAULT now(),
  UNIQUE (event_id, email));
INSERT INTO venues (name, city, capacity) VALUES
  ('Harbor Hall', 'Halifax', 300), ('Pier Theatre', 'Halifax', 120),
  ('Lighthouse Loft', 'Lunenburg', 60);

INSERT INTO events (venue_id, title, starts_at, price)
SELECT 1 + g % 3,
       (ARRAY['Jazz Night', 'Comedy Hour', 'Folk Session', 'Poetry Slam',
              'Sea Shanty Evening', 'Indie Film Night'])[1 + g % 6],
       timestamptz '2026-01-02 19:30' + g * interval '2 days',
       (ARRAY[15, 20, 25, 30])[1 + g % 4]
FROM generate_series(1, 60) g;

SELECT setseed(0.7);
INSERT INTO bookings (event_id, email, seats, booked_at)
SELECT e, 'guest' || c || '@mail.com', 1 + (random() * 2)::int,
       timestamptz '2026-01-01' + random() * interval '100 days'
FROM generate_series(1, 60) e, generate_series(1, 40) c
WHERE random() < 0.5;
Also uses _empty
1

Summary

Window functions and CTEs turn bookings into the owner's monthly report. Next, search for events.

Key takeaways
  • Revenue
  • Share %
  • Fill rate
Next up: Project: Search — find shows by name