l.res_table = 'res_x' and l.user_id = @user and l.res_id = r.res_id and l.sequence > 1 and ((r.status <> 'END' and r.status <> 'DEL' and r.status <> 'ATT' and r.status <> 'VAL') or((l.viewed = 0 or l.viewed = 100) and r.status = 'END')) and is_folder = 'Y'