• Not sure but you may be able to do something like this assuming there is no overlap in dates in your @Vehicle_Depreciations table:

    ;WITH Tally (n) AS (

    SELECT TOP 1000 ROW_NUMBER() OVER (ORDER BY (SELECT NULL))

    FROM sys.all_columns)

    SELECT a.unitnr, depr_date=m, Vehicle_Depreciation_Value --=SUM(Vehicle_Depreciation_Value)

    --SELECT a.unitnr, Vehicle_Depreciation_Value=SUM(Vehicle_Depreciation_Value)

    FROM @Vehicle a

    INNER JOIN @Vehicle_Depreciations b

    ON a.unitnr = b.unitnr

    CROSS APPLY (

    SELECT n, m=DATEADD(month, n-1, Start_Date)

    FROM Tally

    WHERE n BETWEEN 1 AND 1+DATEDIFF(month, Start_Date, End_Date)) c

    WHERE m <= ISNULL(date_block, CAST('2099-01-01' AS DATETIME))

    --GROUP BY a.unitnr

    ORDER BY a.unitnr, m

    If you need total "to date" depreciation for a vehicle, then comment out the SELECT, uncomment the SELECT below it, uncomment the GROUP BY and remove m from the ORDER BY list.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St