WITH `cte` AS (
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY `contact_id` ORDER BY `created_at` DESC
) AS `row`
FROM `contacts_history`
WHERE `created_at` BETWEEN '2021-02-05 15:56:57' AND '2021-02-20 13:44:09'
) SELECT *
FROM `cte`
WHERE `status_id` = 37
AND `row` = 1
data_issued + 10 < curdate()
`date_issued` < CURDATE() - INTERVAL 10 DAY