Prueba asi:
Código SQL:
Ver originalSELECT *, ROW_NUMBER() OVER (partition BY ano,meta,mes ORDER BY ano,meta,mes) x
INTO #rm
FROM resumen_metas
SELECT T0.ano,T0.meta,T0.mes,T1.m1,T2.m2
FROM #rm T0
LEFT JOIN (
SELECT ano,meta,mes,MAX(x) x FROM (SELECT ano,meta,mes,x,m1 FROM #rm) T WHERE m1 >= 0 GROUP BY ano,meta,mes
) T1 ON T0.ano = T1.ano AND T0.meta = T1.meta AND T0.mes = T1.mes AND T0.x = T1.x
LEFT JOIN (
SELECT ano,meta,mes,MAX(x) x FROM (SELECT ano,meta,mes,x,m2 FROM #rm) T WHERE m2 >= 0 GROUP BY ano,meta,mes
) T2 ON T0.ano = T2.ano AND T0.meta = T2.meta AND T0.mes = T2.mes AND T0.x = T2.x