@@ROWCOUNTCASECROSS APPLYCURSORDROP TABLE IF EXISTSINTERSECT & EXCEPTLAG & LEADMERGENOT EXISTSOFFSET FETCHOUTPUTPIVOTRANKSTORED PROCEDURETRY-CATCHWINDOW FUNCTION