

create table migrate_title_transactions as select cvo.id, cvo.purchase_price, cvo.new_owner_id partner_id, cvo.vehicle_id, upper(cvo.curr_ownership_status) curr_ownership_status,
case cvo.security_interest when true then 'Y' else 'N' end security_interest,
case cvo.download when true then 'Y' else 'N' end download,
vv.taxbase,
concat(cvo.invoice_id,'-',cvo.id) as invoice_id, concat(ai.number,'-',cvo.id) as title_number, ai.number as ai_number, ai.amount_total, ai.date_invoice, ai.date_due, ai.date_invoice create_date, upper(ai.state) invoice_status
from customer_vehicle_ownership cvo
join valid_vehicle vv on cvo.vehicle_id = vv.id
join account_invoice ai on cvo.invoice_id  = ai.id
				where  ai.state = 'paid'
                order by cvo.id;

create index migrate_title_transactions_inv_idx on migrate_title_transactions(invoice_id);
