Hi saikandimalla
I am not sure if insert statements given by you are right .
Below query will work for the results that you want .
SEL
cust_id,
src_id ,
branch,
month_end_date,
MAX(MTD) ,
MAX(YTD)
FROM
(
SEL
cust_id,
src_id ,
branch,
ADD_MONTHS( month_end_date -EXTRACT( DAY FROM month_end_date ) , 1 )month_end_date ,
SUM(amt) OVER ( PARTITION BY cust_id ,src_id, branch ,EXTRACT( MONTH FROM month_end_date ) ORDER BY month_end_date ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) MTD,
SUM(amt) OVER ( PARTITION BY cust_id ,src_id, branch ORDER BY month_end_date ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) YTD
FROM sales
)a
GROUP BY 1,2,3,4
Let me know if doesnt work .
Hi saikandimalla
I am not sure if insert statements given by you are right .
Below query will work for the results that you want .
SEL
cust_id,
src_id ,
branch,
month_end_date,
MAX(MTD) ,
MAX(YTD)
FROM
(
SEL
cust_id,
src_id ,
branch,
ADD_MONTHS( month_end_date -EXTRACT( DAY FROM month_end_date ) , 1 )month_end_date ,
SUM(amt) OVER ( PARTITION BY cust_id ,src_id, branch ,EXTRACT( MONTH FROM month_end_date ) ORDER BY month_end_date ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) MTD,
SUM(amt) OVER ( PARTITION BY cust_id ,src_id, branch ORDER BY month_end_date ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) YTD
FROM sales
)a
GROUP BY 1,2,3,4
Let me know if doesnt work .