ScottPletcher (9/5/2012)
Since the table already contains the previous year's YTD totals, would a simple LEFT JOIN suffice to get the prior year's YTD total?
SELECT
t1curr.cod, t1curr.[year], t1curr.[month], t1curr.value, t1prev.value
FROM dbo.Table_1 t1curr
LEFT OUTER JOIN dbo.Table_1 t1prev ON
t1prev.cod = t1curr.cod AND
t1prev.[year] = t1curr.[year] - 1 AND
t1prev.[month] = t1curr.[month]
Assuming "cod" - whatever it is - doesn't change from year to year. If it's a stock ID then the assumption is optimistic.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden