-- PARAMETERS COME FROM USER INPUT AND CAN INCREASE BASED ON REPORTING DECLARE @Date1 DATETIME, @Date2 DATETIME, @Date3 DATETIME, @Date4 DATETIMESET @Date1 = '1991/06/01'SET @Date2 = '1991/06/29'SET @Date3 = '1991/08/01'SET @Date4 = '1991/09/04'DECLARE @Count INT, @Month INT, @TSQL NVARCHAR(4000)SET @Count = 1SET @Month = 5WHILE @Count <= @MonthBEGIN SET @TSQL = 'Select COUNT(*) FROM dbo.titles WHERE pubdate >= @Date' + CONVERT(NVARCHAR(1),@Count) + ' AND pubdate < @Date' + CONVERT(NVARCHAR(1),@Count+1) + '' EXEC (@TSQL) PRINT (@TSQL) SET @Count = @Count + 1END
DECLARE @Date1 DATETIME, @Date2 DATETIME, @Date3 DATETIME, @Date4 DATETIMESET @Date1 = '1991/06/01'SET @Date2 = '1991/06/29'SET @Date3 = '1991/08/01'SET @Date4 = '1991/09/04';with CteDates(Dates)as(Select @Date1 union Select @Date2 union Select @date3 union Select @Date4 -- Thru 255),cteDatePairs(LoDate,HiDate)as(Select cteDates.Dates, nextDate.Dates from cteDates cross apply (Select top 1 innerdates.Dates from cteDates innerdates where innerdates.Dates > cteDates.Dates order by cteDates.Dates) as nextdate where cteDates.Dates is not null)Select cteDatePairs.LoDate,cteDatePairs.HiDate,COUNT(*) FROM dbo.titles, cteDatePairs WHERE pubdate >= cteDatePairs.LoDate AND pubdate < cteDatePairs.HiDategroup by cteDatePairs.LoDate,cteDatePairs.HiDate
DECLARE @Date1 DATETIME, @Date2 DATETIME, @Date3 DATETIME, @Date4 DATETIMESET @Date1 = '1991/06/01'SET @Date2 = '1991/06/29'SET @Date3 = '1991/08/01'SET @Date4 = '1991/09/04';with CteDates(Dates) as
-- declare and set the looping variablesDECLARE @Count INT, @Month INTSET @Count = 1SET @Month = 5-- build a table to hold the datesdeclare @Test TABLE ( RowID int IDENTITY, DateValue datetime)-- insert the test datainsert into @TestSELECT '1991/06/01' UNION ALLSELECT '1991/06/29' UNION ALLSELECT '1991/08/01' UNION ALLSELECT '1991/09/04'-- build a table to hold the titles... only need the pubdate field for this testdeclare @Titles table ( pubdate datetime)-- put @Month # of entries in for each date so we will get some counts.insert into @Titlesselect DateValue from @Test cross join master.dbo.spt_values where type = 'P' and number between 1 and @Month --get separate results per @count - this duplicates what you're doing nowset @Count = 1while @Count <= @Month begin select [Quantity] = COUNT(*) from @Titles t INNER JOIN @Test t1 ON t1.RowID = @Count INNER JOIN @Test t2 ON t2.RowID = @Count + 1 where t.pubdate >= t1.DateValue and t.pubdate < t2.DateValue set @Count = @Count + 1end--get combined results per @count - in case you want the results in one resultset-- I'm using a pseudo-tally table here... you can use a real tally table here if you already have oneselect [Count#] = c.number , [Quantity] = COUNT(*) from master.dbo.spt_values c CROSS JOIN @Titles t INNER JOIN @Test t1 ON t1.RowID = c.number INNER JOIN @Test t2 ON t2.RowID = c.number + 1 where t.pubdate >= t1.DateValue and t.pubdate < t2.DateValue and c.number between 1 and @Month and c.[type] = 'P' group by c.number