How to make a select that combines various results from different wheres?

I am using Postgres as db.

I have a select that brings a total billing of a certain period.

SELECT SUM(vend_vtotal) 
FROM venda 
where vend_data >= ? and vend_data <= ?;

Ex:

SELECT SUM(vend_vtotal) 
FROM venda 
where vend_data >= '01-08-19' 
and vend_data <= '31-08-19';

Returns total August billing

If I want to know the annual invoicing (maker a chart with all monthly invoices) could I make 12 selects? Could .... What I want to know is if there is any way to make a single select that returns me several fields with monthly billings.

Upvotes: 0

Views: 30

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271091

Are you looking for aggregation?

select date_trunc('month', vend_data) as mon, sum(vend_vtotal) as total
from venda v
where vend_data >= '2019-01-01' and vend_data < '2020-01-01'
group by date_trunc('month', vend_data)
order by mon;

Upvotes: 3

Related Questions