-- Collection of code snippets by Arne Vajhøj 
-- (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
CREATE TABLE tbl1 (
    person_id INTEGER,
    navn VARCHAR(50),
    PRIMARY KEY(person_id)
);

INSERT INTO tbl1 VALUES(1,'Per');
INSERT INTO tbl1 VALUES(2,'Klaus');

CREATE TABLE tbl2 (
    person_id INTEGER,
    tlf_nr INTEGER,
    PRIMARY KEY(person_id,tlf_nr)
);
    
INSERT INTO tbl2 VALUES(1,1111);
INSERT INTO tbl2 VALUES(1,1222);
INSERT INTO tbl2 VALUES(1,86868686);
INSERT INTO tbl2 VALUES(2,33);
INSERT INTO tbl2 VALUES(2,111);
INSERT INTO tbl2 VALUES(2,11111);

SELECT tbl1.navn,tbl2_1.tlf_nr,tbl2_2.tlf_nr,tbl2_3.tlf_nr
FROM ((tbl1 JOIN tbl2 tbl2_1 ON tbl1.person_id=tbl2_1.person_id)
JOIN tbl2 tbl2_2 ON tbl1.person_id=tbl2_2.person_id AND tbl2_2.tlf_nr>tbl2_1.tlf_nr)
JOIN tbl2 tbl2_3 ON tbl1.person_id=tbl2_3.person_id AND tbl2_3.tlf_nr>tbl2_1.tlf_nr AND tbl2_3.tlf_nr>tbl2_2.tlf_nr;
