add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
Help with an interesting Postgres question: Why isn't an Index Only Scan used on a partition accessed via the parent table?.

CREATE TABLE my_table
("col1" text, "col2" text, "col3" text, "col4" text)
;
INSERT INTO my_table
("col1", "col2", "col3", "col4")
VALUES
('001', '00A', '00B', '001'),
('001001', '00A', '00B', '001'),
('002', '00X', '00Y', '002'),
('002002', '00X', '00Y', '002')
;
4 rows affected
SELECT DISTINCT ON (col2, col3, col4) *
FROM my_table WHERE col2 = '00A'
ORDER BY col2,
col3,
col4,
CASE
WHEN col1 = '001001' THEN 1
WHEN col4 = '001' THEN 2
END;
col1 col2 col3 col4
001001 00A 00B 001