• I was wondering why you haven't used the CROSS APPLY operator for the first query (see below), any special reason for that?

    SELECT a.quote_date

    , a.close_price

    , dat.[20_day_sma]

    FROM #google_stock a

    CROSS APPLY (

    SELECT AVG(b.close_price) [20_day_sma]

    FROM #google_stock b

    WHERE DateDiff(d, a.quote_date, b.quote_date) BETWEEN -20 AND 0) dat

    ORDER BY a.quote_date