January 5, 2006 at 10:11 am
How do i select records from the previous quarter when it's January?
January 5, 2006 at 10:42 am
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]
January 5, 2006 at 10:57 am
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