Question : populating a column

Table 1
SiteID            TimeFilterID            #customers      customers(weekoverweek)
1            1            1000
2            1            1500
3            1            2000
4            2            900
5            2            1200
6            3            800      

Table 2
TimeFilterId            StartDate      endDate
1            3/2/2009      3/9/2009
2             3/9/2009      3/16/2009
3            3/16/2009      3/23/2009
4
5

Question : I am trying to populate customers(weekoverWeek) column using the formula : [(customers for a given startdate)/(customers for a date a week ago)-1]*100%
...om MS SQL Server

Answer : populating a column

update this
set customersWeekOverWeek = ISNULL((
      select (1.0*this.Customers/nullif(lastwk.Customers,0)-1)*100
      from CustWOW lastwk
      where lastwk.timeFilterID=this.timeFilterID
        and lastwk.SiteID=this.SiteID
      ),0)
from CustWOW this


But for your sample data, it is always 0, because take SiteID 1, TimeFilterID 2.  It is the same value as the last week, such that

(this-last)=0, which ends up with 0% change
Random Solutions  
 
programming4us programming4us