0
It is possible to make a query in which in the same query two sums are executed on the same column?
Here’s what I got:
Total sum of sales
select month(emitido_date) as mes, ifnull(sum((det.preco * det.quantidade) * (iva.valor/100) + (det.preco * det.quantidade) - (det.preco * det.quantidade * (det.desconto/100))),0) as total
from documento as doc
inner join documento_serie as serie on serie.id = doc.documento_serie_id
inner join documento_detail as det on doc.id = det.documento_id
inner join phos_iva as iva on iva.id = det.iva_id
where serie.documento_categoria_id = 3 and doc.rascunho = false and doc.exercicio_id = 4
group by mes
order by mes
Sum Sales Liquidated
select month(emitido_date) as mes, ifnull(sum((det.preco * det.quantidade) * (iva.valor/100) + (det.preco * det.quantidade) - (det.preco * det.quantidade * (det.desconto/100))),0) as total
from documento as doc
inner join documento_serie as serie on serie.id = doc.documento_serie_id
inner join documento_detail as det on doc.id = det.documento_id
inner join phos_iva as iva on iva.id = det.iva_id
where serie.documento_categoria_id = 3 and doc.rascunho = false and doc.exercicio_id = 4 and (serie.documento_tipo_id = 10 or serie.documento_tipo_id = 11 or serie.documento_tipo_id = 15)
group by mes
order by mes
1st question. It is possible to have the return of a table |Month|totalVendas|totalLiquidado| everything only in a query?
Question 2. In the second query presented, in the clause where
, when the field serie.documento_tipo_id
for 15, no inner join
table documento_detail
the field cannot be the doc.id
but rather the doc.source_id
. How can I put this condition?
You already tested those query’s?
– Jorge B.
Yes these query’s are working, only in the second it does not add the value if the document is of type 15.
– Hugo Machado
Hugo changes the 15 of place with the 11 and it verifies if it ceases to add the documents of the type 11.
– Jorge B.
Yes it adds up the 11, only it does not add up the 15 as explained in the second question.
– Hugo Machado
experiment with
and serie.documento_categoria_id IN(10,11,15)
– Jorge B.
I am in mysql and this IN gives error. I don’t know if what I say will help, but doc 15 has nothing that corresponds to it in the table documento_detail, so it can only get its value from doc.source_id
– Hugo Machado
So it’s normal that nothing goes missing.
– Jorge B.
Yes it is normal that nothing goes missing, but I want it to go away, because its value comes from doc.source_id in this specific case.
– Hugo Machado
Let’s go continue this discussion in chat.
– Jorge B.