DROP TABLE PortCoExit;
CREATE TABLE PortCoExit AS
SELECT A.coname, A.statecode, A.datefirstinv, A.datelastinv, A.city, CASE WHEN a.mavsipo=0 THEN 0::int WHEN a.mavsipo=1 ipovsma IS NOT NULL THEN 1::int ELSE NULL0::int END AS mavsipoExit, ipovsma, CASE WHEN a.mavsipoipovsma=0 1 THEN B.announceddate proceedsamt::numeric WHEN a.mavsipoipovsma=1 0 THEN C.issuedate transactionamt::numeric ELSE NULL::date numeric END AS exitdateexitvaluem, CASE WHEN a.mavsipoipovsma=1 THEN issuedate WHEN ipovsma=0 OR a.mavsipo=1 THEN 1::int effectivedate ELSE 0NULL::int date END AS exitedexitdate, CASE WHEN a.mavsipoipovsma=0 1 THEN B.transactionamt extract(year from issuedate) WHEN a.mavsipoipovsma=1 0 THEN C.proceedsamt extract(year from effectivedate) ELSE NULL::money int END AS exitvalueexityear FROM ExitKeysClean companybasecore AS A LEFT JOIN MANoDups ExitKeys AS B ON A.targetnameconame=B.targetname coname AND A.targetstatestatecode=B.targetstate statecode AND A.announceddatedatefirstinv=B.announceddatedatefirstinv LEFT JOIN IPONoDups IPOCleanNoDups AS C ON A.issuernameconame=C.issuer coname AND A.issuerstatestatecode=C.statecode AND A.issuedatedatefirstinv=C.issuedatedatefirstinv LEFT JOIN MACleanNoDups AS D ON A.coname=D.coname AND A.statecode=D.statecode AND A.datefirstinv=D.datefirstinv;
--48001