 CREATE INDEX idx_comphist_prev ON rf21_COMPONENTHISTORY(previousVersion);
 CREATE INDEX idx_comphist_c ON rf21_COMPONENTHISTORY(isConcept);
 
-SELECT * from rf21_COMPONENTHISTORY where componentid = 100343017;
+SELECT * from rf21_COMPONENTHISTORY where componentid = @COMPONENT_OF_INTEREST;
 
 -- Where there is no other change, but the inactivation indicator
 -- is modified, add a new row.


-- Where the term entry in the history has no previous entry in the history, 
-- then we'll mark the status as 0 - Created.
UPDATE rf21_COMPONENTHISTORY ch 
SET CHANGETYPE=0,
STATUS = 0
WHERE isConcept = false
AND NOT EXISTS (
	SELECT 1 from rf2_term_sv tf

  AND ( 
     ch.changeType > ch2.changeType
     OR (ch.changeType = ch2.changeType AND ch.status = 1 AND NOT ch2.status = 1)
     OR (ch.changeType = ch2.changeType AND ch.status = 0 AND NOT ch2.status = 0)
     )
);
