Quantcast
Channel: Teradata Forums - All forums
Viewing all articles
Browse latest Browse all 27759

Can some one provide the solution for the below scenario? - response (4) by ulrich

$
0
0
I think you are looking for something like this...   select base.cust_id, base.src_id, base.branch, base.month_end_date, coalesce(amt,0) as MTD, sum(coalesce(amt,0)) over (partition by base.cust_id, base.src_id, base.branch order by base.month_end_date rows between unbounded preceding and current row) as YTD from ( select b.cust_id, b.src_id, b.branch, c.calendar_date as month_end_date from ( select cust_id, src_id, branch, min(add_months((month_end_date-extract(day from month_end_date)),1)) as min_month_end_date from sales group by 1,2,3 ) as b cross join ( select calendar_date from sys_calendar.calendar where calendar_date = add_months((calendar_date-extract(day from calendar_date)),1) and year_of_Calendar = extract(year from current_date) and calendar_Date <= current_date ) as c where c.calendar_date >= b.min_month_end_date ) base left outer join (select cust_id, src_id, branch, add_months((month_end_date-extract(day from month_end_date)),1) as month_end_date, sum(amt) as amt from sales group by 1,2,3,4 ) as months_amt on base.cust_id = months_amt.cust_id and base.src_id = months_amt.src_id and base.branch = months_amt.branch and base.month_end_date = months_amt.month_end_date P.S. Always specify a PI

Viewing all articles
Browse latest Browse all 27759

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>