Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi,I know I've seen this here somewhere before, but can't find it.I want to chart over any given time period the number of active customers we have (grouped by day). In our customers table we have an activateddate and cancelleddate column so for any given day figuring out the number of active customers is pretty easy. The bit I'm struggling is generating a seqeuntial list of days that I can join on, so that I can create a stored proc with a startdate and enddate as arguments. I need have a sequential list of days in case there are days in the customer table where no-one was activated or cancelled so I don't get gaps in my chart.Thanks
tkizer
Almighty SQL Goddess
38200 Posts
Posted - 2003-10-01 : 20:11:34
Not sure if there is a set-based method...
SET NOCOUNT ONCREATE TABLE Table1 (SomeColumn DATETIME NOT NULL)DECLARE @enddate DATETIMEDECLARE @date DATETIMESET @date = 'Jan 01 2000'SET @enddate = 'Dec 31 2010'WHILE @date <= @enddateBEGIN INSERT INTO Table1 VALUES(@date) SET @date = DATEADD(day, 1, @date)ENDSELECT CONVERT(VARCHAR(50), SomeColumn, 101)FROM Table1
Tara
jsmith8858
Dr. Cross Join
7423 Posts
Posted - 2003-10-01 : 21:12:20
create a tally table, or a table of numbers from 0 to 1000 or so. Store it permanently in your database, it will be useful for situations like this.Then, you can just say something like:SELECT DateAdd(dd,Number, @StartDate) as NewDateFROM NumbersWHERE Number <= DateDiff(dd,@EndDate, @StartDate)or something along those lines.- Jeff