IF object_id('tempdb..#jimmy') is not NULL BEGIN
DROP TABLE #jimmy;
END
IF object_id('tempdb..#jimmy1') is not NULL BEGIN
DROP TABLE #jimmy1;
END SELECT
ROW_NUMBER() OVER(ORDER BY Expr1 DESC) as RowNumber,
E0127,
Expr1,
A0101,
A0177,
A01BN,
codeitemdesc,
C0104
INTO #jimmy
FROM t_teamleader_view
WHERE C0104 IS NOT NULL AND A0177 IS NOT NULL; SELECT *
INTO #jimmy1
FROM
(SELECT *FROM #jimmy
WHERE RowNumber IN
(SELECT max(RowNumber) as validid FROM #jimmy
GROUP BY A0177)) as tb1