Select Previous Qtr''s data in January

  • How do i select records from the previous quarter when it's January?

  • SELECT * FROM table1

    WHERE

    (DATEPART(quarter, @date) = 1 AND YEAR(table1_date) - 1 =  YEAR(@date) AND DATEPART(quarter, @date) = 4)

    OR

    DATEPART(quarter, @date) <> 1 AND YEAR(table1_date) =  YEAR(@date) AND DATEPART(quarter, table1_date) = DATEPART(quarter, @date) - 1 ) OR

     

    and your date is the current date or whatever date you want.

     

    This should give you records from the table1 for any date that is in the last quarter

    ---------------------------------------------
    [font="Verdana"]Nothing is impossible.
    It is just a matter of time and money.[/font]

  • Sorry a little cut and paste typo

    It should be something like this:

    SELECT * FROM table1

    WHERE

    (DATEPART(quarter, @date) = 1 AND DATEPART(quarter, table1_date) = 4 AND YEAR(table1_date) = YEAR(@date) - 1)

    OR

    (DATEPART(quarter, @date) <> 1 AND DATEPART(quarter, table1_date) = DATEPART(quarter, @date) - 1 AND YEAR(table1_date) =  YEAR(@date))

    I don't know how closely does it much your needs but it should give you an idea.

    ---------------------------------------------
    [font="Verdana"]Nothing is impossible.
    It is just a matter of time and money.[/font]

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

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