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;