This should give you correct results, but won't be the fastest thing on the planet on your large dataset.
Indexing could help quite a bit, and/or a computed column with an index on the computation. In particular, a outletid+productid+weekdate composite index.
--use a second table to identify where the breaks > 7 days are
create
table #ptemp (WeekDate datetime NOT NULL, OutletId nvarchar(50) NOT NULL, ProductId nvarchar(50) NOT NULL)
insert
#ptemp (outletid,productID,weekdate)
select
p.outletid,p.productID,p.weekdate
from
#PromoHistory p left outer join #PromoHistory p2
on
p.outletid=p2.outletID and p.productID=p2.productid and p2.weekdate=dateadd(dd,-7,p.weekdate)
where
p2.weekdate is null
--add an index to help performance some
create
index ptmp on #ptemp(outletid,productid)
--get the data
select
ranktmp,min(WeekDate) startdate , max(dateadd(dd,7,weekdate)) endweek, OutletId, ProductId, Price, Cost,PromotionType,AdType,DisplayType,[X Value],[Y Value]
from
(select ph.*, count(pt.weekdate) ranktmp from #promohistory ph inner join #ptemp pt on ph.OutletId=pt.outletid and ph.ProductId=pt.productid
where
pt.weekdate<=ph.weekdate
group
by
ph.WeekDate, ph.OutletId, ph.ProductId, Price, Cost,PromotionType,AdType,DisplayType,[X Value],[Y Value]
)
ptmp
group
by OutletId, ProductId, Price, Cost,PromotionType,AdType,DisplayType,[X Value],[Y Value], ranktmp
order
by outletid, productid,ranktmp, min(WeekDate),max(weekdate)
--clean up and remove secondary table
drop
table #ptemp
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?