I think you are going to be stuck with dynamic SQL if these results must be as columns based on date range input... I feel like this query is becoming too complicated for such an easy resultset :) Perhaps take a look at the db design as well, but here is example:set nocount oncreate table #attendance (student_name varchar(15), subject varchar(15), attendance_date datetime)insert into #attendance select 'ABC','English','1-1-2005' union select 'ABC','Math','1-1-2005' union select 'BCD','English','1-1-2005' union select 'ABC','English','1-2-2005' union select 'BCD','English','1-3-2005' union select 'ABC','Math','1-4-2005' union select 'BCD','English','1-4-2005' union select 'ABC','English','1-5-2005' union select 'BCD','English','1-5-2005' -- select * from @attendancedeclare @start_date datetime, @end_date datetime, @cur_date datetime, @cmd varchar(8000)select @start_date = '1-1-2005', @end_date = '1-5-2005' select @cur_date = @start_date, @cmd = 'SELECT student_name, 'while @cur_date < @end_datebegin select @cmd = @cmd + 'SUM(CASE WHEN attendance_date = ''' + CONVERT(VARCHAR,@cur_date) + ''' THEN 1 ELSE 0 END) AS ''' + CONVERT(VARCHAR,@cur_date) + ''',' select @cur_date = (@cur_date + 1)endselect @cmd = substring(@cmd,1,len(@cmd)-1) + ' from #attendance group by student_name'exec (@cmd)--print @cmddrop table #attendance