Wednesday, March 28, 2012

How to show result for each day of the past 30 days?

Hello,

I would like to show the result fo each day of the past 30 days. I might not have data to display for everyday but I still want the date to show up
01/27/2007 resultA
01/28/2007 resultB
01/29/2007 .
...
02/26/2007 resultC
02/27/2007 resultD

Thanks for your help
Arnold

If there is a possibility there could be no records for each day in the column, you might have to build this table manually. If the records do exist you can do a GROUP BY convert(Varchar,datefield, 101). You can probably create a stored proc , use a table variable with the 2 columns and insert data into it and finally do a SELECT from it.|||

Could you give me an example for the table variable as I have no idea on how to build that.

Thanks

|||

Just to give you an idea here's a sample. You might need to build your code on top of this (if this works for you).

declare @.ttable( Datecoldatetime, col2varchar(50))Declare @.mindatedatetime, @.maxDatedatetime, @.daysint,Set @.mindate ='01/27/2007'Set @.maxDate ='02/27/2007'while (datediff(d,@.mindate,@.maxdate) >= 0 )BeginInsert into @.t (Datecol, col2)Values (@.mindate )set @.mindate = @.mindate + 1--Another Query to get the results for date = @.mindateEndselect *from @.t

No comments:

Post a Comment