If I have a table like this called orders:
id date value client_name
1 01/02/2017 50 Name 1
2 02/02/2017 60 Name 2
3 02/02/2017 60 Name 2
4 03/02/2017 20 Name 2
And a query which gets me details about the orders for a given day:
Select
sum(value) as total_order_value,
count(id) as number_of_orders
from orders
where orders.date = '02/03/2017'::date
Is there a way to construct a single query which will execute that query for a range of dates, giving one row per date executed for?
I've used something like this in the past:
select date(a),
(SUB QUERY WHICH RETURNS A SINGLE VALUE)
FROM generate_series(
(CURRENT_DATE - interval '2 months')::date,
CURRENT_DATE,
'1 day'
) s(a)
But this only works if the subquery returns a single value. I'm looking for a way to do this when the subquery returns multiple columns (such as in the first example above).
So from the first example above, a query which would generate the result set:
date total_order_value number_of_orders
01/02/2017 50 1
02/02/2017 120 2
03/02/2017 20 1
04/02/2017 0 0
05/02/2017 0 0
etc