• P.S. If number of tables can possibly vary i'd better opt to FULL JOIN instead of LEAD /LAG.

    I don't think FULL JOIN would be required. LEAD/LAG should still work because they will return NULL values (which can be defaulted to 0 to make the math work) for non-matching offset-rows.

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service