WITH conta_fin AS (
	SELECT	ecf.id_conta_financeira
	FROM	conta_financeira AS cf
		INNER JOIN empresa_conta_financeira AS ecf ON (ecf.id_conta_financeira = cf.id_conta_financeira)
	WHERE	ecf.id_empresa = :IdEmpresaObrigatorio)

SELECT	r.codigo,
	r.denominacao,
	SUM(r.saldo_disponivel) AS saldo_disponivel
FROM	(
	SELECT cf.codigo,
		cf.denominacao,
		SUM(mf.valor_movimento * (POSITION(CAST('1' AS CHAR) IN CAST(tmf.entrada_saida AS CHAR(1))) * 2 - 1)) AS saldo_disponivel
	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)
	WHERE	mf.data_movimento <= :DataFinal 
	AND cf.id_conta_financeira in (499,695727,698832,731774)
	GROUP BY 1,2
     ) AS r
GROUP BY r.codigo, r.denominacao
ORDER BY 
	r.denominacao