WITH sub AS (
	SELECT 
		COALESCE(SUM(rm.valor_entrada - rm.valor_saida), 0) AS saldo_banco
	FROM	resumo_movimento_financeiro rm
		INNER JOIN conta_financeira AS cf ON (cf.id_conta_financeira = rm.id_conta_financeira)
		LEFT OUTER JOIN banco AS b ON (b.id_banco = cf.id_banco)
		INNER JOIN sis_empresa AS se ON (se.id_empresa = rm.id_empresa)
	WHERE rm.ano_mes < (EXTRACT('YEAR' FROM DATE(DATE_TRUNC('month', CAST(:Data AS DATE)))) * 100 + EXTRACT('MONTH' FROM DATE(DATE_TRUNC('month', CAST(:Data AS DATE)))) )
	AND rm.id_empresa IN (:IdEmpresaMultSlc)
	AND cf.tipo IN (2,3,4)

	UNION ALL 

	SELECT	
		COALESCE(SUM(mf.valor_movimento * (POSITION(CAST('1' AS CHAR) IN CAST(tmf.entrada_saida AS CHAR(1))) * 2 - 1)), 0) AS saldo_banco
	FROM	movimento_financeiro AS mf
		INNER JOIN tipo_movimento_financeiro AS tmf ON (tmf.id_tipo_movimento_financeiro = mf.id_tipo_movimento_financeiro)
		INNER JOIN conta_financeira AS cf ON (cf.id_conta_financeira = mf.id_conta_financeira)
		LEFT OUTER JOIN banco AS b ON (b.id_banco = cf.id_banco)
		INNER JOIN sis_empresa AS se ON (se.id_empresa = mf.id_empresa)
	WHERE	mf.id_empresa IN (:IdEmpresaMultSlc)
	AND	mf.data_movimento BETWEEN DATE(DATE_TRUNC('month', CAST(:Data AS DATE))) AND CAST(:Data AS DATE)
	AND	cf.tipo IN (2,3,4)
)
SELECT 
		COALESCE(SUM(s.saldo_banco), 0) saldo_banco
FROM sub s