2
I need to compile a report for a client with the billing for each day of a given month.
Until then beauty, the problem is that now he wants even the days that have no sale, are shown in the table with the value showing zero.
For example, this one of mine query day month and year and makes a date.
I know there are easier ways but the version of Firebird is old because it already has another system using the same.
Anyway this is mine query
SELECT EXTRACT(DAY FROM p.DATAFECHAMENTO) || '/' || EXTRACT(MONTH FROM p.DATAFECHAMENTO) || '/' || EXTRACT(YEAR FROM p.DATAFECHAMENTO) AS data
, SUM(pp.valor) AS total
FROM PEDIDOPAGAMENTO AS pp
INNER JOIN PEDIDO AS p ON pp.codpedido = p.codpedido
WHERE EXTRACT(MONTH FROM p.DATAFECHAMENTO) = @Mes
AND EXTRACT(YEAR FROM p.DATAFECHAMENTO) = @Ano
GROUP BY data
That one query returns the following
These are the days that had something sold, however I wanted to show the days that were not sold anything also with the total = 0
Ex:
01/05/2017 0
02/05/2017 0
03/05/2017 0
...
First, do these days without sale have in the table? If they have, you can change the
inner join
to allow bringing data from one table even without corresponding data in the other, zeroing the value. If you don’t have it, you’ll have to do a query that generates those dates in memory or in a temporary table and do the Join from this table. Another question:firebird
ormysql
? has both tags on your question– Ricardo Pontual
Not in the table, this is the b.o, about generating the dates in memory would have a good article or something like that showed me? By the way is Firebird
– Anderson Henrique
I don’t know how to create this in
firebird
, but as a temporary table, the answer below @Joãomartins does what I suggested, it generates a table with the dates and do theleft join
with it, should solve the problem– Ricardo Pontual