SELECT * FROM (
SELECT dev_id, di_channel, di_preset, di_capture_time, di_filename, RANK() OVER (
PARTITION BY dev_id, di_channel, di_preset
ORDER BY di_capture_time DESC
) seq
FROM tb_device_image
WHERE di_capture_time > DATE_SUB(NOW(), INTERVAL 3 DAY)
) a
WHERE seq < 4