Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- extract sales by day, store, product
- select business_date, store_code, product_code,
- sum(sales) as sales
- into #temp1
- from sales_table
- where business_date >= date_trunc('month', dateadd('month', -24, sysdate))
- group by business_date, store_code, product_code;
- -- compare this year against last year
- select ty.*, ly.sales as sales_ly
- into #temp2
- from #temp1 ty left join #temp1 ly
- on ty.product_code = ly.product_code
- and ty.store_code = ly.store_code
- and trunc(dateadd(year, -1, ty.business_date)) = ly.business_date;
- -- check
- select to_char(business_date, 'yyyymm'), sum(sales) ty, sum(sale_ly) as ly
- from #temp2
- group by to_char(business_date,'yyyymm')
- order by 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement