Hi there,I want to return specific rows within a date range from a table using parameter.So for example:__________________________declare @tblInfo table(name varchar(50), PeriodStart datetime, Periodend datetime, value int)insert into @tblInfo (name, PeriodStart,Periodend,value)values ('J','1 jan 2012','31 jan 2012',1),('J','1 feb 2012','29 feb 2012',2),('J','1 mar 2012','31 mar 2012',3),('J','1 may 2012','31 may 2012',4),('J','1 jun 2012','30 jun 2012',5),('A','1 jan 2012','31 jan 2012',1),('A','1 feb 2012','29 feb 2012',2),('A','1 mar 2012','31 mar 2012',3),('A','1 may 2012','31 may 2012',4),('A','1 jun 2012','30 jun 2012',5)declare @period datetimeset @period = '2012-05-01'select name, value, periodstart,periodendfrom @tblInfowhere @Period between periodstart and periodend
Which returns:name value periodstart periodendJ 4 2012-05-01 2012-05-31 A 4 2012-05-01 2012-05-31
I also want to return the previous periodstart, PreviousEnd and value for each 'Name' as well, like so:name value periodstart periodend Previousvalue PreviousPeriodstart PreviousPeriodendJ 4 2012-05-01 2012-05-31 3 2012-03-01 2012-03-31 A 4 2012-05-01 2012-05-31 3 2012-03-01 2012-03-31
The last period is not always last month, it could be 6 months or more since the last period.I understand there are many ways to do this but what would be the best way in your opinion. Thanks