-- 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 (
    v INTEGER NOT NULL,
    PRIMARY KEY(v)
)
GO

INSERT INTO t VALUES(3)
GO
INSERT INTO t VALUES(4)
GO
INSERT INTO t VALUES(5)
GO
INSERT INTO t VALUES(6)
GO
INSERT INTO t VALUES(7)
GO
INSERT INTO t VALUES(10)
GO
INSERT INTO t VALUES(11)
GO
INSERT INTO t VALUES(12)
GO
INSERT INTO t VALUES(13)
GO
INSERT INTO t VALUES(14)
GO
INSERT INTO t VALUES(24)
GO
INSERT INTO t VALUES(25)
GO
INSERT INTO t VALUES(26)
GO
INSERT INTO t VALUES(27)
GO
INSERT INTO t VALUES(28)
GO
INSERT INTO t VALUES(31)
GO

SELECT * FROM t
GO

SELECT MIN(v) AS z FROM t
UNION
SELECT * FROM (SELECT TOP 8 v AS z FROM t WHERE v NOT IN (SELECT MIN(v) FROM t) AND v NOT IN (SELECT MAX(v) FROM t) ORDER BY CAST(SQRT(524287*v) AS INTEGER)%31) x
UNION
SELECT MAX(v) AS z FROM t
ORDER BY z
GO

DROP TABLE t
GO
