Select latest two records

I have data in a table that has an indicator to show which month it showed up.  I am trying to find a way to select the last two months of data in the table.
FairfieldAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
SELECT * FROM urTable where IndicatorColumn >= Month(Getdate() )- 2 and IndicatorColumn < Month(getdate() )
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HwkrangerCommented:
Example:
-- Example Table
DECLARE @temp Table (ID int, DateStamp datetime)
-- Populate Example Table
Declare @i int
SET @i = 365
WHILE @i > 0
BEGIN
	insert into @temp values (@i, dateadd(d, -1 * @i, getdate()))
	SET @i = @i  - 1
END
 
-- Example Query from Example Table
SELECT * From @temp
WHERE DateStamp >= dateadd(m, -2, getdate())

Open in new window

0
BrandonGalderisiCommented:
That won't work aneesh because if you run it in January, month(1)-2 wouild be -1 which is invalid.
SELECT * FROM urTable 
where IndicatorColumn >= dateadd(m, datediff(m, 0, getdate())-2,0)
and indicatorColumn <= dateadd(m, datediff(m, 0, getdate()),0)

Open in new window

0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

BrandonGalderisiCommented:
Fairfield:

I think you jumped too soon.  That one will not work.
0
HwkrangerCommented:
The previous example showed the last 2 months, being subtract 2 months.  If you want the last 2 months with the month beginning at the first day of the month:


-- Example Table
DECLARE @temp Table (ID int, DateStamp datetime)
-- Populate Example Table
Declare @i int
SET @i = 365
WHILE @i > 0
BEGIN
	insert into @temp values (@i, dateadd(d, -1 * @i, getdate()))
	SET @i = @i  - 1
END
 
-- Example Query from Example Table
-- Gets last 2 months, with the first date retrieved as 2 months before today
SELECT * From @temp
WHERE DateStamp >= dateadd(m, -2, getdate())
 
-- Example Query from Example Table
-- Gets last 2 months, with the first date retrieved as the first day of the month
SELECT *
 From @temp
WHERE DateStamp >= convert(varchar(2), Month(dateadd(m, -2, getdate()))) + '/01/' + convert(varchar(4), year(dateadd(m, -2, getdate())))

Open in new window

0
HwkrangerCommented:
LoL.  
0
Aneesh RetnakaranDatabase AdministratorCommented:
I agree with Brandon
0
BrandonGalderisiCommented:
Fairfield:
I'd like you to review http:#a24442793 and consider accepting http:#a24442793 as the answer.  Even http:#a24442759 will not work because it will not give you two full months.  It will give you from two months ago which means on the 21-May it will give 21-Mar, not 01-Mar.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.