0
Good guys, I have a situation where I need to show the balance of billing per client, as I could in no way use a select that combines the result of two other select, I decided to use the separate results and perform the direct subtraction operation in ASP instead of using SQL for this. However some customers have no value to pay, just receive or otherwise, and this brings a serious problem in my grid, customers who do not have one of these values inherit from the previous one who has this field X filled. For example:
The top line customer has the two correct values, but the second line does not have this 25 USD in the system, only the other value.
The point is, this happens because I use two selects based on the customers that are in the invoice listing, is it possible for me to force a value of 0 when the customer does not appear in select? Example of select below and code.
SELECT
ID_CD_PESSOA,
NM_PAGREC,
COALESCE(SUM(COALESCE(VL_FATURA_TOTAL_MOEDA, 0)), 0) AS TOTAL_APAGAR,
DS_MOEDA_PRINCIPAL
FROM (SELECT
FATU.ID_CD_FATURA,
FATU.ID_CD_BOOKING,
FATU.ID_CD_QUEM_PAGA,
FATU.ID_CD_QUEM_RECEBE,
FATU.ID_CD_FATURA_TIPO,
FATU.IN_STATUS,
FATU.ID_CD_FATURA_REL,
NM_PAGREC = (SELECT
CASE
WHEN CRMP.IN_TIPO = 'F' THEN CRMP.NM_PESSOA + ' ' + CRMP.NM_SOBRENOME + '<br>' + CONVERT(varchar, CRMP.NR_CPF_CNPJ)
ELSE CRMP.NM_APELIDO + '<br>' + CONVERT(varchar, CRMP.NR_CPF_CNPJ)
END
FROM TB_CRM_PESSOAS_CRMP CRMP
WHERE ID_CD_PESSOA = FATU.ID_CD_QUEM_PAGA
OR ID_CD_PESSOA = FATU.ID_CD_QUEM_RECEBE),
ID_CD_PESSOA = (SELECT
CASE
WHEN CRMP.IN_TIPO = 'F' THEN CRMP.ID_CD_PESSOA
ELSE CRMP.ID_CD_PESSOA
END
FROM TB_CRM_PESSOAS_CRMP CRMP
WHERE ID_CD_PESSOA = FATU.ID_CD_QUEM_PAGA
OR ID_CD_PESSOA = FATU.ID_CD_QUEM_RECEBE) -- TIPO DE FATURA
,
FATU.FL_CONV_MOEDA_BRL -- BOOKING
,
BOOK.DS_AMS_REFERENCE -- ADICIONAIS
,
TEM_FECHAMENTO.FL_TEM_FECHAMENTO,
TEM_FECHAMENTO.ID_CD_FATURA_PRI -- FINANCEIRO
,
TEM_LANCAMENTO.ID_CD_LANCAMENTO,
TEM_LANCAMENTO.FL_COMPENSADO,
COALESCE(VL_FATURA_TOTAL, 0) AS VL_FATURA_TOTAL,
COALESCE(VL_FATURA_TOTAL_MOEDA, 0) AS VL_FATURA_TOTAL_MOEDA,
DS_MOEDA_PRINCIPAL = (SELECT TOP (1)
CASE
WHEN FATU.ID_CD_QUEM_PAGA IS NOT NULL THEN FAIT.DS_MOEDA_VENDA
ELSE FAIT.DS_MOEDA_CUSTO
END DS_MOEDA_PRINCIPAL
FROM TB_FATURA_ITENS_FAIT FAIT
WHERE FAIT.ID_CD_FATURA = FATU.ID_CD_FATURA)
FROM TB_FATURA_FATU FATU
INNER JOIN TB_BOOKING_BOOK BOOK
ON BOOK.ID_CD_BOOKING = FATU.ID_CD_BOOKING
INNER JOIN TB_FATURAS_TIPOS_FATP FATP
ON FATP.ID_CD_FATURA_TIPO = FATU.ID_CD_FATURA_TIPO
INNER JOIN TB_LOGIN_LOGI LOGI
ON LOGI.ID_CD_LOGIN = FATU.ID_CD_LOGIN
OUTER APPLY (SELECT TOP 1
FL_TEM_FECHAMENTO = 'S',
FAPR2.ID_CD_FATURA_PRI
FROM TB_FATURA_AUX_FPAX FPAX2
INNER JOIN TB_FATURA_PRINCIPAL_FAPR FAPR2
ON FAPR2.ID_CD_FATURA_PRI = FPAX2.ID_CD_FATURA_PRI
WHERE FPAX2.ID_CD_FATURA = FATU.ID_CD_FATURA
AND FAPR2.IN_STATUS = 1) AS TEM_FECHAMENTO
OUTER APPLY (SELECT TOP 1
ID_CD_LANCAMENTO,
FL_COMPENSADO = (SELECT TOP 1
'N'
FROM TB_FIN_LANCAMENTOS_PARC_FNLP FNLP2
WHERE FNLP2.ID_CD_LANCAMENTO = FNLP.ID_CD_LANCAMENTO
AND FNLP2.ID_CD_AUXILIAR = FATU.ID_CD_FATURA
AND FNLP2.FL_AUXILIAR = 'FAT'
AND FNLP2.FL_SITUACAO = 'A'),
DT_ULTIMO_PAGTO = (SELECT TOP 1
FNLP3.DT_COMPENSADO
FROM TB_FIN_LANCAMENTOS_PARC_FNLP FNLP3
WHERE FNLP3.ID_CD_LANCAMENTO = FNLP.ID_CD_LANCAMENTO
AND FNLP3.ID_CD_AUXILIAR = FATU.ID_CD_FATURA
AND FNLP3.FL_AUXILIAR = 'FAT'
AND (FNLP3.FL_SITUACAO = 'C'
OR FNLP3.FL_SITUACAO = 'G')
ORDER BY ID_CD_PARCELA DESC)
FROM TB_FIN_LANCAMENTOS_PARC_FNLP FNLP
WHERE FNLP.ID_CD_AUXILIAR = FATU.ID_CD_FATURA
AND FNLP.FL_AUXILIAR = 'FAT'
AND FNLP.FL_CANCELADO = 'N') AS TEM_LANCAMENTO
WHERE ID_CD_FATURA <> '') TB
WHERE (((ID_CD_FATURA_TIPO = '3'
AND IN_STATUS = '1')
AND (ID_CD_FATURA_PRI IS NULL
AND ID_CD_LANCAMENTO IS NULL))
AND ID_CD_QUEM_RECEBE <> '')
AND ID_CD_QUEM_RECEBE = @ID_CD_PESSOA
GROUP BY ID_CD_PESSOA,
DS_MOEDA_PRINCIPAL,
NM_PAGREC,
DS_MOEDA_PRINCIPAL
Check here, this topic already exists.... https://stackoverflow.com/questions/3997327/return-0-if-field-is-null-in-mysql
– Ernesto Casanova
As you already use COALESCE in your query I believe that the title of your question does not reflect your real doubt.
– anonimo
Build virtual tables and make a Join , I think the solution https://forum.imasters.com.br/topic/490784-calcular-values-entre-duas-tables/? do=findComment&comment=1950875
– Motta
Thanks Ernesto and Motta, I’ll be trying with the tips you gave. Mr. Anonimo, the point is that as you can see even using the COALESCE did not work for what I needed, this function only brings the first result out of null that it finds, I need the void, a non-existent query to be brought as anything, it is now clear?
– Pietro Rey