Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
begin
   declare @day  int
   set @day = 2

   declare @days varchar(8000)
   set @days = '[1]'
   -- Declare final column names for Pivot
   Declare @columnsday NVARCHAR(MAX)
   set @columnsday = 'isnull(cast([1] as varchar(10)),'''') as [1]'
   WHILE (@day <= 31)
   BEGIN
       set @days = @days + ', ' + quotename(convert(varchar, @day))
       set @columnsday = @columnsday + ',ISNULL(cast(' + quotename(convert(varchar, @day)) + ' as varchar(10)) ,'''') as '+ quotename(convert(varchar, @day))
       set @day  = @day + 1
       --print @days
   END
   --print @days
   --print @columnsday

   DECLARE @query varchar(8000)
   set @query = 'SELECT StudentCurrId,StudentCode,Stuname,' + @columnsday  + 'from
     (SELECT tbl_StudentAttendanceDetails.StuAttendId,tbl_StudentAttendanceDetails.StudentCurrId,
     StudentCode,upper(First_Name + '' '' + Middle_Name + '' '' + Last_Name) as Stuname,
     day(StudentCurrId) as day,Stuattend FROM tbl_StudentAttendanceDetails
     inner join
     tbl_StudentAttendanceMaster
     on tbl_StudentAttendanceDetails.StuAttendId=tbl_StudentAttendanceMaster.StuAttendId
   inner join tbl_Student_Current
   on tbl_StudentAttendanceDetails.StudentCurrId=tbl_Student_Current.StuCurrentId
   inner join StudentAdmission on tbl_Student_Current.StudentId=StudentAdmission.StudentId
       where tbl_StudentAttendanceMaster.SessionId=18'++'
         ) AS SourceTable

      pivot (
            MAX(Stuattend)
               for day in (' + @days +')
             )p'

 print('pivoted report')
exec(@query)
end



output can show this one
=====================
Id name 1 2 3
1 kkp A
2 kkpo P

currently date 19/12/2014
can not show 19 column
Posted
Comments
Tomas Takac 19-Dec-14 3:07am    
Not clear. What is the problem? Could you elaborate?
krishna97 19-Dec-14 3:34am    
Prisent,Abset can show date wise
Tomas Takac 19-Dec-14 3:40am    
How many times did you post this actually? The latest was already closed as repost now I found out the same was already answered here: Solve this query error[^]. Please note it's considered rude reposting the same question over and over again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900