Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 month query

Author  Topic 

thanksfor help
Posting Yak Master

106 Posts

Posted - 2009-04-24 : 18:44:04
I need to get the first and last 3 months in rolling 12 months data. month field is in varchar(10) format.

For given month : 2009 04

Rolling 12 months will be
2008 05
2008 06
2008 07
2008 08
2008 09
2008 10
2008 11
2008 12
2009 01
2009 02
2009 03
2009 04

I was successfull in generating 12 months and need help in getting first 3 and last 3 months.

thanks

dsindo
Starting Member

45 Posts

Posted - 2009-04-24 : 19:32:36
create table #temp (mo varchar(10))
insert into #temp values( '2008 05')
insert into #temp values('2008 06')
insert into #temp values('2008 07')
insert into #temp values('2008 08')
insert into #temp values('2008 09')
insert into #temp values('2008 10')
insert into #temp values('2008 11')
insert into #temp values('2008 12')
insert into #temp values('2009 01')
insert into #temp values('2009 02')
insert into #temp values('2009 03')
insert into #temp values('2009 04')



select top 3 mo as fisrt_3_months from #temp order by mo desc
select top 3 mo as last_3_months from #temp order by mo asc
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-04-25 : 00:13:41
[code]
select dateadd(month, n, '2009-04-01')
from (
select n = 0 union all
select n = -1 union all
select n = -2 union all
select n = -11
) n[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

thanksfor help
Posting Yak Master

106 Posts

Posted - 2009-04-27 : 12:15:36
thanks, it worked !!!!
Go to Top of Page
   

- Advertisement -