-- 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 (
    id INTEGER NOT NULL PRIMARY KEY,
    kroner DECIMAL(12,2),
    land CHAR(2),
    dato DATE
);

INSERT INTO t VALUES(1, 43, 'DK', 20061212);
INSERT INTO t VALUES(2, 45, 'DK', 20070302);
INSERT INTO t VALUES(3, 43, 'NO', 20070302);
INSERT INTO t VALUES(4, 100, 'UK', 20070411);
INSERT INTO t VALUES(5, 100, 'SV', 20060223);
INSERT INTO t VALUES(6, 87, 'SP', 20071009);
INSERT INTO t VALUES(7, 34, 'SP', 20070707);

SELECT AVG(kroner)
FROM t t1
WHERE dato = (SELECT MAX(dato) FROM t t2 WHERE t2.land=t1.land);

DROP TABLE t;
