4
I need to consult the number of banknotes issued with template 55 to create a report. The problem is that I’m needing to put zeroed values in the quantity column even though it has no value at all.
Ex:
empresa filial data quantidade
5 1 01/2012 30
5 1 02/2012 42
5 1 03/2012 30
5 1 04/2012 52
5 2 01/2012 33
5 2 02/2012 42
However, I’ve consulted all year, specifically I’d like it to stay that way:
empresa filial data quantidade
5 1 01/2012 30
5 1 02/2012 42
5 1 03/2012 30
5 1 04/2012 52
5 1 05/2012 0
5 1 06/2012 0
5 1 07/2012 0
5 1 08/2012 0
5 1 09/2012 0
5 1 10/2012 0
5 1 11/2012 0
5 1 12/2012 0
5 2 01/2012 33
5 2 02/2012 42
5 2 03/2012 0
5 2 04/2012 0
5 2 05/2012 0
5 2 06/2012 0
5 2 07/2012 0
5 2 08/2012 0
5 2 09/2012 0
5 2 10/2012 0
5 2 11/2012 0
5 2 12/2012 0
I’ve tried with coalesce(quantidade, 0)
, case when(quantidade is null) then quantidade = 0
and even some procedures, but I failed miserably.
In summary:
It is possible to "force" the count()
also inform the null values as 0 even without information?
Edit:
Consultations being held:As requested I will put as I am doing, would put a fictitious code, but I will inform a real, I believe it is clearer. SQL query:
SELECT E.EMPRESA,F.NOMEFANTASIA,F.FILIAL,
EXTRACT(MONTH FROM S.DATASAIDA)||'/'||EXTRACT(YEAR FROM S.DATASAIDA) AS DATASAIDA,
S.MODELO, COUNT(*) AS QUANTIDADE
FROM ES02_SAIDA S
INNER JOIN SC00_FILIAL F ON (F.IDFILIAL = S.IDFILIAL)
INNER JOIN SC00_EMPRESA E ON (E.IDEMPRESA = F.IDEMPRESA)
WHERE E.EMPRESA IN (:IDEMPRESA)
AND S.DATASAIDA BETWEEN ''||:DATAINI||'' AND ''||:DATAFIM||''
AND (S.MODELO = '55' OR S.MODELO = '57')
GROUP BY E.EMPRESA,F.NOMEFANTASIA,F.FILIAL,
EXTRACT(MONTH FROM S.DATASAIDA)||'/'||EXTRACT(YEAR FROM S.DATASAIDA),
S.MODELO
ORDER BY 5,1,2
Upshot:
EMPRESA NOMEFANTASIA FILIAL DATASAIDA MODELO QUANTIDADE
5 EMPRESA 1 1/2012 55 44
5 EMPRESA 1 2/2012 55 34
5 EMPRESA 1 3/2012 55 53
5 EMPRESA 1 4/2012 55 48
5 EMPRESA 1 5/2012 55 76
5 EMPRESA 1 6/2012 55 39
5 EMPRESA 1 7/2012 55 22
5 EMPRESA 1 8/2012 55 34
5 EMPRESA 1 9/2012 55 32
5 EMPRESA 1 10/2012 55 39
5 EMPRESA 1 11/2012 55 38
5 EMPRESA 1 12/2012 55 77
5 EMPRESA2 2 1/2012 55 38
5 EMPRESA2 2 2/2012 55 59
5 EMPRESA2 2 3/2012 55 53
5 EMPRESA2 2 4/2012 55 8
Edit 2:
Good afternoon, thank you very much for your help and attempt to resolve my issue. But I come back with another question to you that would be about using theUNION
.
I am using the following SQL:
SELECT TABELA.EMPRESA,
TABELA.NOMEFANTASIA,
TABELA.FILIAL,
TABELA.DATASAIDA,
TABELA.MODELO,
COUNT(TABELA.MODELO) AS QUANTIDADE
FROM(
SELECT E.EMPRESA,F.NOMEFANTASIA,F.FILIAL,
EXTRACT(MONTH FROM S.DATASAIDA)||'/'||EXTRACT(YEAR FROM S.DATASAIDA) AS DATASAIDA,
S.MODELO, COUNT(*) AS QUANTIDADE
FROM ES02_SAIDA S
INNER JOIN SC00_FILIAL F ON (F.IDFILIAL = S.IDFILIAL)
INNER JOIN SC00_EMPRESA E ON (E.IDEMPRESA = F.IDEMPRESA)
WHERE E.EMPRESA IN (:IDEMPRESA)
AND S.DATASAIDA BETWEEN ''||:DATAINI||'' AND ''||:DATAFIM||''
AND (S.MODELO = '55' OR S.MODELO = '57')
group by 1,2,3,4,5
UNION
SELECT distinct NULL AS EMPRESA,'TOTAIS POR FILIAL' AS NOMEFANTASIA,F.filial AS FILIAL,
null AS DATASAIDA,null as MODELO,
--'QTD='||(SUM(1))||' MÉDIA= '||(SUM(1)/COUNT(S.modelo)) AS QUANTIDADE
(SUM(1)/COUNT(S.modelo)) AS QUANTIDADE
FROM ES02_SAIDA S
INNER JOIN SC00_FILIAL F ON (F.IDFILIAL = S.IDFILIAL)
INNER JOIN SC00_EMPRESA E ON (E.IDEMPRESA = F.IDEMPRESA)
WHERE E.EMPRESA IN (:IDEMPRESA)
AND S.DATASAIDA not BETWEEN ''||:DATAINI||'' AND ''||:DATAFIM||''
AND (S.MODELO = '55' OR S.MODELO = '57')
group by 1,2,3,4,5
)TABELA
GROUP BY
TABELA.EMPRESA,
TABELA.NOMEFANTASIA,
TABELA.FILIAL,
TABELA.DATASAIDA,
TABELA.MODELO
ORDER BY
TABELA.FILIAL asc nulls last,
TABELA.DATASAIDA asc nulls last,
TABELA.EMPRESA asc nulls last
The result is bringing the same results of the first Edit, but the amount is 1 in all and 0 in the totaling line.
EMPRESA NOMEFANTASIA FILIAL DATASAIDA MODELO QUANTIDADE
5 EMPRESA 1 1/2012 55 1
5 EMPRESA 1 10/2012 55 1
5 EMPRESA 1 11/2012 55 1
5 EMPRESA 1 12/2012 55 1
5 EMPRESA 1 2/2012 55 1
5 EMPRESA 1 3/2012 55 1
5 EMPRESA 1 4/2012 55 1
5 EMPRESA 1 5/2012 55 1
5 EMPRESA 1 6/2012 55 1
5 EMPRESA 1 7/2012 55 1
5 EMPRESA 1 8/2012 55 1
5 EMPRESA 1 9/2012 55 1
TOTAIS POR FILIAL 1 0
5 EMPRESA 2 1/2012 55 1
5 EMPRESA 2 2/2012 55 1
5 EMPRESA 2 3/2012 55 1
5 EMPRESA 2 4/2012 55 1
TOTAIS POR FILIAL 2 0
It was supposed to bring 44,34,53... ( Thank you all for your help
Hello Gaspar, could you post an example with some data and an example query (click the edit link on your question)? Apparently you have one
group by empresa, filial, data
right? Usually this involves aLEFT JOIN
with the dates... Even if you have to generate this list of "months in each year" dynamically.– Anthony Accioly
It seems unlikely to me that you will achieve this. If I understand correctly you want to select what you do not have. If you issue a specific command by selecting the date x then the return will be null and you could deal with the coalition, but if you ask to group, it will add up only those that exist, of course. You need another reset table and then you can do Join with it or a case to put intelligence in this select.
– Reginaldo Rigo
Hello, thanks for helping Anthony and Reginaldo.
LEFT JOIN
because it is not an existing column in the table. I tried to do with aLEFT JOIN ES02_SAIDA S2 ON (S2.IDSAIDA = S.IDSAIDA)
but it didn’t work out either.group by
is discarding the results you should report.UNION
and it didn’t work either...– Gaspar
See if you can help http://forum.imasters.com.br/topic/531584-comort-results-zerates/
– Motta
Really did not give, in this case the jamersondr in the forum imasters. The logic is for days, and does not confer date, only years. Even reporting this case in the 12 months would cause the
group by
disregard the zeroed values. Fought for information– Gaspar
I managed to partially arrange the consultation. Thanks for your help
– Gaspar