Paste this into a new question or an answer at dba.stackexchange.com:
<!-- --> > CREATE TABLE customers ( > id int NOT NULL, > company varchar(255), > first_name varchar(255) default NULL, > last_name varchar(255) default NULL, > PRIMARY KEY (id) > ); > > CREATE TABLE transactions ( > id int NOT NULL, > form_type varchar(255) default NULL, > customer_id int default NULL, > due decimal default NULL, > PRIMARY KEY (id) > ); > > INSERT INTO customers (id, company, first_name, last_name) VALUES > (1, 'MDD', 'T', 'H'), > (2, 'Aliance Magnet A LLP', 'A', 'Wilkinson'), > (3, 'MAF', 'C', 'G'), > (4, 'QL', 'F', 'B'), > (5, 'ARL', 'S', 'P'), > (6, 'Q Corp.', 'H', 'H'), > (7, 'VQDA', 'L', 'W'), > (8, 'AESC', 'E', 'W'), > (9, 'Placement Incorporated', 'C', 'Mendez'), > (10, 'Numpties United', 'Y', 'Cunningham'); > > INSERT INTO transactions (id, form_type, customer_id, due) VALUES > (1, 'invoice', 9, 1.08), > (2, 'payment', 1, 6.32), > (3, 'invoice', 7, 9.42), > (4, 'payment', 9, 3.58), > (5, 'invoice', 7, 5.35), > (6, 'payment', 3, 5.42), > (7, 'invoice', 9, 5.32), > (8, 'invoice', 9, 9.62), > (9, 'invoice', 10, 1.40), > (10, 'invoice', 2, 3.72); > > <pre> > ✓ > > ✓ > 10 rows affected > 10 rows affected > </pre> <!-- --> > SELECT > t.id AS trans_id, > c.id AS customer_id, > c.company, > c.first_name, > c.last_name, > t.due > FROM ( > SELECT DISTINCT c.* > FROM customers AS c > JOIN transactions AS t ON t.customer_id = c.id > WHERE t.due > 0 > AND t.form_type = 'invoice' > ORDER BY c.company, c.first_name, c.last_name > LIMIT 3 > ) AS c > JOIN transactions AS t ON t.customer_id = c.id > WHERE t.due > 0 AND t.form_type = 'invoice' > ORDER BY c.company, c.first_name, c.last_name; > > <pre> > trans_id | customer_id | company | first_name | last_name | due > -------: | ----------: | :--------------------- | :--------- | :--------- | ---: > 10 | 2 | Aliance Magnet A LLP | A | Wilkinson | 3.72 > 9 | 10 | Numpties United | Y | Cunningham | 1.40 > 1 | 9 | Placement Incorporated | C | Mendez | 1.08 > 7 | 9 | Placement Incorporated | C | Mendez | 5.32 > 8 | 9 | Placement Incorporated | C | Mendez | 9.62 > </pre> <!-- --> > SELECT > t.id AS trans_id, > c.id AS customer_id, > c.company, > c.first_name, > c.last_name, > t.due > FROM ( > SELECT DISTINCT c.* > FROM customers AS c > JOIN transactions AS t ON t.customer_id = c.id > WHERE t.due > 0 > AND t.form_type = 'invoice' > ORDER BY c.company, c.first_name, c.last_name > LIMIT 3 > ) AS c > JOIN transactions AS t ON t.customer_id = c.id > -- WHERE t.due > 0 AND t.form_type = 'invoice' > ORDER BY c.company, c.first_name, c.last_name; > > <pre> > trans_id | customer_id | company | first_name | last_name | due > -------: | ----------: | :--------------------- | :--------- | :--------- | ---: > 10 | 2 | Aliance Magnet A LLP | A | Wilkinson | 3.72 > 9 | 10 | Numpties United | Y | Cunningham | 1.40 > 1 | 9 | Placement Incorporated | C | Mendez | 1.08 > 4 | 9 | Placement Incorporated | C | Mendez | 3.58 > 7 | 9 | Placement Incorporated | C | Mendez | 5.32 > 8 | 9 | Placement Incorporated | C | Mendez | 9.62 > </pre> *db<>fiddle [here](https://dbfiddle.uk/?rdbms=postgres_11&fiddle=67e589fc21a355844639705507f495ab)*
back to fiddle