con los valores que pones(obvio sin todas las columnas nada mas usando las columnas llaves que manejas) hice lo siguiente:
Código SQL:
Ver originalCREATE TABLE #generales
(
id INT,
estado VARCHAR(20),
total INT
)
CREATE TABLE #partida(
id INT,
folio INT,
depto INT
)
INSERT INTO #generales VALUES (459,'FIN',1)
INSERT INTO #generales VALUES (460,'FIN',2)
INSERT INTO #generales VALUES (461,'NVA',3)
INSERT INTO #generales VALUES (462,'NVA',4)
INSERT INTO #generales VALUES (463,'NVA',5)
INSERT INTO #generales VALUES (464,'NVA',6)
INSERT INTO #generales VALUES (465,'NVA',7)
INSERT INTO #generales VALUES (466,'NVA',8)
INSERT INTO #generales VALUES (467,'NVA',9)
INSERT INTO #generales VALUES (468,'NVA',10)
INSERT INTO #generales VALUES (469,'NVA',11)
INSERT INTO #generales VALUES (470,'NVA',12)
INSERT INTO #generales VALUES (471,'FIN',13)
INSERT INTO #generales VALUES (472,'NVA',14)
INSERT INTO #generales VALUES (473,'NVA',15)
INSERT INTO #generales VALUES (474,'NVA',16)
INSERT INTO #generales VALUES (475,'NVA',17)
INSERT INTO #generales VALUES (476,'NVA',18)
INSERT INTO #generales VALUES (477,'NVA',19)
INSERT INTO #partida VALUES (576,459,65013)
INSERT INTO #partida VALUES (577,460,65015)
INSERT INTO #partida VALUES (578,460,65015)
INSERT INTO #partida VALUES (579,471,65013)
INSERT INTO #partida VALUES (580,471,65022)
INSERT INTO #partida VALUES (581,474,65022)
INSERT INTO #partida VALUES (582,475,65011)
INSERT INTO #partida VALUES (583,478,65013)
INSERT INTO #partida VALUES (584,478,65015)
INSERT INTO #partida VALUES (586,478,65015)
INSERT INTO #partida VALUES (594,479,65013)
INSERT INTO #partida VALUES (595,479,65013)
INSERT INTO #partida VALUES (596,479,65015)
INSERT INTO #partida VALUES (597,480,65013)
INSERT INTO #partida VALUES (598,481,65013)
INSERT INTO #partida VALUES (599,482,65013)
INSERT INTO #partida VALUES (600,483,65013)
INSERT INTO #partida VALUES (601,484,65013)
INSERT INTO #partida VALUES (602,485,65013)
INSERT INTO #partida VALUES (603,485,65013)
INSERT INTO #partida VALUES (604,485,65015)
INSERT INTO #partida VALUES (605,486,65013)
INSERT INTO #partida VALUES (605,486,65014)
INSERT INTO #partida VALUES (605,486,65014)
INSERT INTO #partida VALUES (606,477,65012)
INSERT INTO #partida VALUES (607,477,65012)
SELECT depto,SUM(g_total) FROM (
SELECT depto,isnull(SUM(total),0) g_total FROM #partida AS t1
LEFT JOIN #generales AS t2 ON (t1.folio=t2.id)
GROUP BY t2.id,folio,depto) AS tabla GROUP BY depto
y obtengo como resultado:
65011 17
65012 38
65013 14
65014 0
65015 4
65022 29
que es lo que buscas no??