Rolling 13 weeks of data

  • I'm sure this is simple, I'm just missing something here. I have a table of data, and I want to always select the last 13 weeks. That part I got ok with this:

    Select *

    FROM vw_QA_Quality_excel_NewRate

    WHERE WeekFrom > dateadd(wk,-13,getdate())

    But the problem is that the data is not always updated, so 13 weeks from getdate() may not always show 13 weeks of data. So I'm just trying to first query my date column and find the most recent date, then count 13 weeks back from that, so there's always 13 weeks of actual data. I'm trying something like this, but SQL doesnt' like it, giving an error on the keyword 'Select'

    DECLARE @LASTFULLWEEK DATETIME

    SET @LASTFULLWEEK = Select MAX(WeekFrom) AS LASTFULLWEEK;

    Select *

    FROM vw_QA_Quality_excel_NewRate

    WHERE WeekFrom > dateadd(wk,-13,@LASTFULLWEEK)

    Any help is greatly appreciated!

  • ericb1 (5/22/2012)


    I'm sure this is simple, I'm just missing something here. I have a table of data, and I want to always select the last 13 weeks. That part I got ok with this:

    Select *

    FROM vw_QA_Quality_excel_NewRate

    WHERE WeekFrom > dateadd(wk,-13,getdate())

    But the problem is that the data is not always updated, so 13 weeks from getdate() may not always show 13 weeks of data. So I'm just trying to first query my date column and find the most recent date, then count 13 weeks back from that, so there's always 13 weeks of actual data. I'm trying something like this, but SQL doesnt' like it, giving an error on the keyword 'Select'

    DECLARE @LASTFULLWEEK DATETIME

    SET @LASTFULLWEEK = Select MAX(WeekFrom) AS LASTFULLWEEK;

    Select *

    FROM vw_QA_Quality_excel_NewRate

    WHERE WeekFrom > dateadd(wk,-13,@LASTFULLWEEK)

    Any help is greatly appreciated!

    DECLARE @LASTFULLWEEK DATETIME

    Select @LASTFULLWEEK = MAX(WeekFrom) FROM vw_QA_Quality_excel_NewRate;

  • Ahh, thank you 🙂

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply