Skip to content

Instantly share code, notes, and snippets.

@milinmestry
Last active June 9, 2017 07:12
Show Gist options
  • Save milinmestry/2847ee64b70ba000442f5e0f62ed6675 to your computer and use it in GitHub Desktop.
Save milinmestry/2847ee64b70ba000442f5e0f62ed6675 to your computer and use it in GitHub Desktop.
In the table we have duplicate rows for data to be processed by cron script. Base on latest date(s) from the table values; the script needs to process rows as either add it into another table or delete it.
Problem Scenerio:
================
A) System add new records to be processed for futher into another database table.
B) System update exiting records to be deleted from the current database table.
The process A and/or B can be happened any number of times. So system needs to
process each record for an entity depending on it is recently added or deleted.
SQL:
====
SELECT c.`id`, c.`ct_domain_id`, c.`domain_name`, c.`ct_user_id`, c.`api_status_code`, c.`added_at`, c.`deleted_at`,
IF(`added_at` > IFNULL(`deleted_at`, 0), 'A', 'D') AS `add_or_del`
FROM `qq_domains_sale` AS c
INNER JOIN (
SELECT MAX(`id`) AS `id` FROM `qq_domains_sale`
GROUP BY `ct_domain_id`
) AS d
ON c.`id`= d.`id`
WHERE `ct_user_id`=5035
ORDER BY `ct_domain_id` ASC
Thanks:
https://stackoverflow.com/questions/18221999/group-by-having-max-date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment