Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Calculate YTD and MTD by Period

$
0
0
Hi everyone,
  
I've been beating my head against the wall since last night trying to figure this one out.  I know I'm missing something obvious but I can't seem to get it.  Here's the scenario:
 
I've got an aggregate table that is like this:
 
FISCAL_YEAR, FISCAL_PERIOD, CUSTOMER, SALES_REP, PRODUCT, SALES_AMT, YTD_SALES, QTD_SALES
 
PRODUCT_ID is the lowest level in the table, so that products roll up to sales reps and customers.  I need to calculate the YTD and QTD values based on the period values for SALES_AMT. 
 
I've seen some techniques that use a cursor or a correlated sub-query, but if any of you guys have a script or a code snippet that does this I'd appreciate it.  I think you know what I'm getting at, but here's an example:
 
PERIOD           PRODUCT_ID       AMT          YTD_AMT
JAN                PRODUCT 1           10               10
FEB                PRODUCT 1           20                30
MAR                PRODUCT 1          10                40
 
etc...
 
Thanks in advance everyone.

Viewing all articles
Browse latest Browse all 23857

Trending Articles



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