CREATE OR REPLACE TABLE engineer_contributions AS
SELECT CONCAT(users.lname, ', ', users.fname)                            AS `Engineer`,
       project_lifecycle.project_number                                  AS `Project Number`,
       project_lifecycle.timestamp,
       new_value                                                         AS `Action Type`,
       CONCAT(SUBSTR(project_lifecycle.project_number, 1, 3), ' - ',
              IF(ap.proj_type = 'Warranty', 'Structural', ap.proj_type)) AS `Region`,
       clients.name                                                      AS `Client`,
       ap.description                                                    AS `Description`
FROM users
         INNER JOIN project_lifecycle ON pkey = modifier
         INNER JOIN all_projects ap on project_lifecycle.project_number = ap.refnum
         INNER JOIN contacts on ap.contact_fkey = contacts.pkey
         INNER JOIN clients on contacts.cl_fkey = clients.pkey
WHERE engineer_contributions.`Project Number` in (SELECT DISTINCT `Project Number` from engineer_contributions)
  AND project_lifecycle.new_value IN ('+SEALED')
  AND users.priv & POW(2, 25) > 0;

ALTER TABLE engineer_contributions
    ADD COLUMN id INT AUTO_INCREMENT KEY FIRST;

DELETE
FROM engineer_contributions
WHERE id IN (SELECT bad_rows.id
             FROM engineer_contributions AS bad_rows
                      INNER JOIN (SELECT engineer_contributions.`Project Number`, MAX(id) as min_id
                                  FROM engineer_contributions
                                  GROUP BY engineer_contributions.`Project Number`
                                  HAVING count(*) > 1) AS good_rows
                                 ON good_rows.`Project Number` =
                                    bad_rows.`Project Number`
                                     AND good_rows.min_id <> bad_rows.id);

ALTER TABLE engineer_contributions DROP COLUMN IF EXISTS id;