Prezados boa tarde!
Consegui unir as SQLs que preciso de saldo inicial e despesas por dia, porem eu preciso eliminar o valor zero.
SQL
SELECT
ITENS.CONTA,
SUM (ITENS.SALDOINICIAL)AS SALDOINICIAL,
SUM (ITENS.VALOR)AS VALOR,
SUM (ITENS.SALDOINICIAL) + SUM(ITENS.VALOR) AS SALDO,
ITENS.DATA AS DATA
FROM
(
SELECT
FXCX.CODCXA AS 'CONTA',
'0' AS 'SALDOINICIAL',
SUM (VALOR) AS 'VALOR',
CONVERT (VARCHAR(10), DATACOMPENSACAO , 103) AS 'DATA'
from FXCX (nolock)
inner join FCXA (nolock)
ON(FXCX.CODCOLCXA = FCXA.CODCOLIGADA
and FXCX.CODCXA = FCXA.CODCXA)
where fxcx.DATAESTORNO is null
and FXCX.DATACANCELAMENTO is null
and fxcx.COMPENSADO = 1
and fxcx.TIPO <> '10'
and fxcx.codcoligada = '4'
and fxcx.codfilial = '1'
and fxcx.DATACOMPENSACAO >= case when convert(date, '01/11/2019', 103) <= convert(date, '29/01/2019', 103) then convert(date, '01/11/2019' , 103) END
and fxcx.DATACOMPENSACAO <= case when convert(date, '11/11/2019' , 103) <= convert(date, '29/01/2019', 103) then convert(date, '11/11/2019' , 103)
ELSE convert(date, '29/01/2019', 103) end
and FXCX.CODCXA = '0156'
and fxcx.CODCOLCXA = '4'
GROUP BY
FXCX.CODCXA ,
FCXA.DESCRICAO ,
FXCX.DATACOMPENSACAO
UNION ALL
SELECT
FXCX.CODCXA AS 'CONTA',
'0' AS 'SALDOINICIAL',
SUM (VALOR) AS 'VALOR',
CONVERT (VARCHAR(10), DATACOMPENSACAO , 103) AS 'DATA'
from FXCX (nolock)
inner join FCXA (nolock)
ON(FXCX.CODCOLCXA = FCXA.CODCOLIGADA
and FXCX.CODCXA = FCXA.CODCXA)
where/* fxcx.DATAESTORNO is null
and */FXCX.DATACANCELAMENTO is null
and fxcx.COMPENSADO = 1
and fxcx.TIPO <> '10'
and fxcx.codcoligada = '4'
and fxcx.codfilial = '1'
and fxcx.DATACOMPENSACAO >= case when convert(date, '01/11/2019' , 103) > convert(date, '29/01/2019' , 103) then convert(date, '01/11/2019' , 103)
else convert(date, '29/01/2019', 103) end
and fxcx.DATACOMPENSACAO <= case when convert(date, '11/11/2019' , 103) > convert(date, '29/01/2019' , 103) then convert(date, '11/11/2019' , 103) END
and FXCX.CODCXA = '0156'
and fxcx.CODCOLCXA = '4'
GROUP BY
FXCX.CODCXA,
FCXA.DESCRICAO ,
FXCX.DATACOMPENSACAO
UNION ALL
SELECT
DISTINCT [CODCXA] AS 'CONTA',
[INICIAL] AS 'SALDOINICIAL',
'0' AS 'VALOR',
CONVERT (VARCHAR(10), DATA , 103) AS 'DATA'
FROM [BI].[dbo].[FinanceiroSaldoCompensado]
WHERE CODCXA = '0156'
AND DATA >= '2019-11-01'
)
ITENS(CONTA,SALDOINICIAL,VALOR,DATA)
GROUP BY
CONTA,
DATA
Resultado
Não pode trazer valor despesas com 0
Conta Inicial Despesas Saldo Data
0156 641288,8 -200503.6600440785,1401/11/2019
0156 641288,8 0.0000 641288,8 02/11/2019
0156 1082073,94 0.0000 1082073,9403/11/2019
0156 440785,14 -2342.0800 438443,06
04/11/2019
Resultado Esperado:
Conta Inicial Despesas Saldo Data
0156 641288,8 -200503.6600440785,1401/11/2019
0156 440785,14 -2342.0800 438443,06
04/11/2019
Muito obrigado a todos pela ajuda.