By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE tablename (
`listing` INTEGER,
`stage` INTEGER,
`status` INTEGER,
`timeInMillis` BIGINT
);
INSERT INTO tablename (`listing`, `stage`, `status`, `timeInMillis`) VALUES
('121', '0', '0', '1653429998443'),
('121', '0', '0', '1653429998453'),
('121', '0', '1', '1653429998696'),
('121', '0', '1', '1653429998753'),
('121', '1', '0', '1653429998794'),
('121', '1', '0', '1653429998811'),
('121', '1', '1', '1653429999069'),
('121', '1', '1', '1653429999090');
Records: 8 Duplicates: 0 Warnings: 0
SELECT listing, stage,
MAX(CASE WHEN status = 1 THEN timeInMillis END) -
MIN(CASE WHEN status = 0 THEN timeInMillis END) AS difference
FROM tablename
GROUP BY listing, stage;
listing | stage | difference |
---|---|---|
121 | 0 | 310 |
121 | 1 | 296 |