oracle - SQL Aggregate on Two tables -
table has millions of records 2014, using oracle
id sales_amount sales_date 1 10 20/11/2014 1 10 22/11/2014 1 10 22/12/2014 1 10 22/01/2015 1 10 22/02/2015 1 10 22/03/2015 1 10 22/04/2015 1 10 22/05/2015 1 10 22/06/2015 1 10 22/07/2015 1 10 22/08/2015 1 10 22/09/2015 1 10 22/10/2015 1 10 22/11/2015
table b
id id_date 1 22/11/2014 2 01/12/2014
i want sum of totals 6 months 1 year id 1 taking starting
date table b 22/11/2014
output sales_amount_6months sales_amount_6months 1 70 130
shall use add_months
in case?
yes, can use add_months()
, conditional aggregation :
select b.id, sum(case when a.sales_date between b.id_date , add_months(b.id_date,6) a.sales_amount else 0 end) sales_6_month, sum(case when a.sales_date between b.id_date , add_months(b.id_date,12) a.sales_amount else 0 end) sales_12_month tableb b join tablea on(b.id = a.id) group b.id
Comments
Post a Comment