Les envio la consulta del Kardex, asumiendo que las tablas #TmpKardexV22 y #TmpKardexV11 tienen la misma info.
Cita:
SELECT *, ((select SUM(canting)
FROM #TmpKardexV11
Where numreg<=t1.numreg and idmatprima=t1.idmatprima)-
(select SUM(cantsal)
FROM #TmpKardexV11
Where numreg<=t1.numreg and idmatprima=t1.idmatprima)) as saldito,
((select SUM(imping)
FROM #TmpKardexV11
Where numreg<=t1.numreg and idmatprima=t1.idmatprima)-
(select SUM(impsal)
FROM #TmpKardexV11
Where numreg<=t1.numreg and idmatprima=t1.idmatprima)) as valtot,
((select SUM(imping)
FROM #TmpKardexV11
Where numreg<=t1.numreg and idmatprima=t1.idmatprima)-
(select SUM(impsal)
FROM #TmpKardexV11
Where numreg<=t1.numreg and idmatprima=t1.idmatprima))/
((select SUM(canting)
FROM #TmpKardexV11
Where numreg<=t1.numreg and idmatprima=t1.idmatprima)-
(select SUM(cantsal)
FROM #TmpKardexV11
Where numreg<=t1.numreg and idmatprima=t1.idmatprima)) as costprom
from #TmpKardexV22 t1
FROM #TmpKardexV11
Where numreg<=t1.numreg and idmatprima=t1.idmatprima)-
(select SUM(cantsal)
FROM #TmpKardexV11
Where numreg<=t1.numreg and idmatprima=t1.idmatprima)) as saldito,
((select SUM(imping)
FROM #TmpKardexV11
Where numreg<=t1.numreg and idmatprima=t1.idmatprima)-
(select SUM(impsal)
FROM #TmpKardexV11
Where numreg<=t1.numreg and idmatprima=t1.idmatprima)) as valtot,
((select SUM(imping)
FROM #TmpKardexV11
Where numreg<=t1.numreg and idmatprima=t1.idmatprima)-
(select SUM(impsal)
FROM #TmpKardexV11
Where numreg<=t1.numreg and idmatprima=t1.idmatprima))/
((select SUM(canting)
FROM #TmpKardexV11
Where numreg<=t1.numreg and idmatprima=t1.idmatprima)-
(select SUM(cantsal)
FROM #TmpKardexV11
Where numreg<=t1.numreg and idmatprima=t1.idmatprima)) as costprom
from #TmpKardexV22 t1
Haber ayudenme.........