-- 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 t (pk INTEGER PRIMARY KEY, v1 INTEGER, v2 INTEGER, v3 VARCHAR(50))
GO

INSERT INTO t VALUES(1, 11, 12, 'Bla bla')
GO
INSERT INTO t VALUES(2, 11, 12, 'Der var engang')
GO
INSERT INTO t VALUES(3, 21, 22, '...')
GO

SELECT * FROM t
GO

SELECT v1,v2,MAX(pk) maxpk FROM t GROUP BY v1,v2
GO

SELECT pk FROM t WHERE pk NOT IN (SELECT maxpk FROM (SELECT v1,v2,MAX(pk) maxpk FROM t GROUP BY v1,v2) tagg)
GO

DELETE FROM t WHERE pk NOT IN (SELECT maxpk FROM (SELECT v1,v2,MAX(pk) maxpk FROM t GROUP BY v1,v2) tagg)
GO

SELECT * FROM t
GO

DROP TABLE t
GO
