HEX
Server: Apache/2.2.15 (CentOS)
System: Linux ip-10-0-2-146.eu-west-1.compute.internal 2.6.32-754.35.1.el6.centos.plus.x86_64 #1 SMP Sat Nov 7 11:33:42 UTC 2020 x86_64
User: root (0)
PHP: 5.6.40
Disabled: NONE
Upload Files
File: /www/exchange2/sql/archive/2016-03-03-391.sql
ALTER TABLE `investor_investments`  ADD COLUMN `is_latest` TINYINT(3) UNSIGNED NULL DEFAULT '0';

DROP TEMPORARY TABLE IF EXISTS latest_investments;
CREATE TEMPORARY TABLE latest_investments (
     latest_investment_id MEDIUMINT(20) UNSIGNED NOT NULL,
     investor_id MEDIUMINT(20) UNSIGNED NOT NULL,
     investment_group MEDIUMINT(20) UNSIGNED NOT NULL,
     INDEX `invoice_id` (`investor_id`,`latest_investment_id`,`investment_group`)
)
COLLATE="utf8_general_ci"
ENGINE=InnoDB
ROW_FORMAT=DEFAULT;

INSERT INTO latest_investments
SELECT MAX(investment_id) as latest_investment_id, investor_id,investment_group
FROM investor_investments
GROUP BY investor_id,investment_group ;

UPDATE investor_investments SET is_latest=0;

UPDATE investor_investments 
LEFT JOIN latest_investments ON latest_investments.investor_id = investor_investments.investor_id AND latest_investments.latest_investment_id = investor_investments.investment_id AND investor_investments.investment_group = latest_investments.investment_group
SET is_latest=1
WHERE latest_investments.latest_investment_id IS NOT NULL;

DROP TEMPORARY TABLE IF EXISTS latest_investments;