Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. -- extract sales by day, store, product
  2. select business_date, store_code, product_code,
  3. sum(sales) as sales
  4. into #temp1
  5. from sales_table
  6. where business_date >= date_trunc('month', dateadd('month', -24, sysdate))
  7. group by business_date, store_code, product_code;
  8.  
  9.  
  10. -- compare this year against last year
  11. select ty.*, ly.sales as sales_ly
  12. into #temp2
  13. from #temp1 ty left join #temp1 ly
  14. on ty.product_code = ly.product_code
  15. and ty.store_code = ly.store_code
  16. and trunc(dateadd(year, -1, ty.business_date)) = ly.business_date;
  17.  
  18. -- check
  19. select to_char(business_date, 'yyyymm'), sum(sales) ty, sum(sale_ly) as ly
  20. from #temp2
  21. group by to_char(business_date,'yyyymm')
  22. order by 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement