select
`access_histories`.`id` as `history_id`,
`people`.`id` as `person_id`,
`access_histories`.`access_status` as `access`, `access_histories`.`accessed_location` as `location`, `access_histories`.`created_at` as `datetime`, CONCAT(people.first_name, " ", people.last_name) as full_name, COALESCE(NULLIF(people.email, ""), users.email) as email, IFNULL(people.gender, 'n_a') as gender,
`people`.`source` as `source`,
people.affiliation, `access_histories`.`reason`
from `access_histories`
inner join `people` on `people`.`id` = `access_histories`.`person_id`
left join `users` on `people`.`id` = `users`.`person_id` where
`access_histories`.`created_at` >= '2023-01-01 09:18:00' and `access_histories`.`created_at` <= '2023-12-05 09:18:59'
order by `datetime` desc
limit
25 offset 0