SQL Pivot Result

Dear Experts,
I have a attached store procedure to display records as Pivot Table. this is showing perfect data only creating issue to convert PaidUnPaid Column into converting

if i replaced 'Paid' to -1 then its run perfectly
When i placed 'Paid' instead of numeric its not run

My SQL is as follows:
Drop Table #tempFeePaidUnPaidDetail

Select a.StudentId
  , b.GRNNo
  , b.ParentId
  , b.StudentName
  , c.FatherName
 , Class = e.ClassName  
  , Section = f.SectionName
  , Shift = g.ShiftName  
  , d.RollNo 
  , MonthNames= left(case when ftm.MonthNo is null then DateName( month , DateAdd( month , a.MonthNo, -1 )) else DateName( month , DateAdd( month , ftm.MonthNo, -1 )) end  ,3)
   , PaidUnPaid=Case When (a.Credit -Isnull((select Debit=SUM(debit) from FeeTransactionsMonth xx 
				Where xx.MonthNo=a.monthNo and xx.FiscalYearId=a.fiscalyearid and xx.TransId=a.TransId Group by TransID, MonthNo,FiscalYearId),0))=0 then
				'Paid' else CONVERT(Varchar(10),a.Credit) end
    --case when ftm.TransId is null then a.Credit 
	--				 When (a.Credit - isnull(ftm.Debit,0)) = 0  then '0'
	--				 Else (a.Credit - isnull(ftm.Debit,0)) end
	,MonthNo = case when ftm.MonthNo is null then convert(varchar,a.MonthNo) else convert(varchar,ftm.MonthNo) end
Into #tempFeePaidUnPaidDetail
from FeeTransactions a
Join Students b on a.StudentId =b.StudentId 
Left Outer Join Parents c on b.ParentId =c.ParentID 
Left Outer Join StudentYearRecord d on a.StudentId =d.StudentId and a.CampusId=d.CampusID and a.FiscalYearId=d.YearID
Join Classes e  on d.PC=e.ClassId 
Join Sections f on d.Section=f.SectionId 
join Shifts g on d.Shift=g.ShiftId 
left join FeeTransactionsMonth ftm on (ftm.TransId = a.TransId)
Where a.Credit >0 
and a.FiscalYearID=1
and (b.PresentClassId like 1)
--and (b.PresentSectionId like @SectionId)
--and (b.PresentShiftId like @ShiftId)
--and (b.GRNNo like @GRNNo)
and b.FeeTerm not in (2)
Order by e.ClassName  
  , f.SectionName 
  , g.ShiftName, d.RollNo
Exec CrossTabMake 'Select StudentId,GRNNo,ParentId,StudentName,FatherName,Class,Section,Shift,RollNo,MonthNames,MonthNo,PaidUnPaid From #tempFeePaidUnPaidDetail'

Open in new window

/****** Object:  StoredProcedure [dbo].[CrossTabMake]    Script Date: 01/22/2014 22:05:19 ******/
ALTER procedure [dbo].[CrossTabMake] ( 
@SQL nvarchar(4000), 
@PivotCol varchar(100), 
@Summaries varchar(100), 
@GroupBy varchar(100), 
@OtherFields varchar(100) = '', 
@OrderByFields varchar(100) = '', 
@rollup bit = 0, 
@Debug bit = 0) 

--Exec SearchAllTables 'CrossTabMake'
set nocount on 
set ansi_warnings off 

declare @Vals varchar(8000); 
declare @Vals2 varchar(8000); 
declare @Vals3 varchar(8000); 
declare @tmp varchar(1000); 
declare @TotalLen int; 
declare @roll varchar(20); 

set @Vals = ''; 
set @Vals2 = ''; 
set @Vals3 = ''; 
set @TotalLen = len(@SQL) + len(@GroupBy) + Len(@OtherFields) + Len(@OrderByFields) 

if (@OtherFields <> '') 
set @OtherFields = ', ' + @OtherFields 

if (@OrderByFields <> '') 
set @OrderByFields = ' order by ' + @OrderByFields 

set @roll = CASE @rollup WHEN 1 THEN ' WITH ROLLUP' ELSE '' END 

create table #temp (thisPivot varchar(100)) 

insert into #temp 
exec ('select distinct convert(varchar(100),' + @PivotCol + ') as thisPivot FROM (' + @SQL + ') A') 
select @tmp = 
replace(replace(@Summaries,'(','(CASE WHEN ' + @PivotCol + '=''' + replace(thisPivot,'''','''''') + 
''' THEN '),')[', ' END) as [' + thisPivot ), 
@TotalLen = @TotalLen + Len(@tmp), 
@Vals = case when @TotalLen < 7800 then @Vals + ', ' + @tmp else @Vals end, 
@Vals2 = case when @TotalLen between 7800 and 15799 then @Vals2 + ', ' + @tmp else @Vals2 end, 
@Vals3 = case when @TotalLen between 15800 and 23799 then @Vals3 + ', ' + @tmp else @Vals3 end 
order by 

drop table #Temp 

if (@Debug=0) 
exec( 'select ' + @GroupBy + @OtherFields + @Vals + @Vals2 + @Vals3 + 
' from (' + @SQL + ') A GROUP BY ' + @GroupBy + @roll + @OrderByFields) 
print( 'select ' + @GroupBy + @OtherFields + @Vals + @Vals2 + @Vals3 + 
' from (' + @SQL + ') A GROUP BY ' + @GroupBy + @roll + @OrderByFields) 

set nocount off 
set ansi_warnings on

Open in new window

Who is Participating?
Kevin CrossChief Technology OfficerCommented:
It is as I suspected.  It will not work that way.  If you only expect one value, you could try MAX as the aggregate.  If you need the SUM, then leave paid as zero, so SUM will be zero.  That way you can use COALESCE(CONVERT(VARCHAR(10), NULLIF(pivot_column, 0)), 'Paid') on each of the pivoted columns.
MehramAuthor Commented:
Kevin CrossChief Technology OfficerCommented:
I may not be following the logic completely, but it appears you are passing to your procedure the first query and asking it to SUM(PaidUnPaid) column.  Therefore, if you make it 'Paid' in the first query, it should not work.  Try making this column just the difference in debts and credits as a number value, then in the final select use CASE statement (or ISNULL-NULLIF combination) to replace 0 with 'Paid'.
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

MehramAuthor Commented:
I have tried to do something like this but no luck

Exec CrossTabMake 'Select StudentId,GRNNo,ParentId,StudentName,FatherName,Class,Section,Shift,RollNo,MonthNames,MonthNo,
					PaidUnPaid=Case When PaidUnPaid>0 then PaidUnPaid else ''-0'' end From #tempFeePaidUnPaidDetail'

Open in new window

Kevin CrossChief Technology OfficerCommented:
What is the error message it is giving you?  To debug, you can PRINT out the dynamic SQL you are creating.  You may spot the issue more easily that way.
MehramAuthor Commented:
I have change SQL

Please to see screen with error
MehramAuthor Commented:
MehramAuthor Commented:
Sir can you change it my query
MehramAuthor Commented:
Its worked sir
expected value was only one

Exec CrossTabMake 'Select StudentId,GRNNo,ParentId,StudentName,FatherName,Class,Section,Shift,RollNo,MonthNames,MonthNo,
					PaidUnPaid=Case When PaidUnPaid=0 then ''Paid'' else Convert(Varchar(10),PaidUnPaid) end From #tempFeePaidUnPaidDetail'

Open in new window

Kevin CrossChief Technology OfficerCommented:
I am glad you got it and that I could help.

Best regards and happy coding,

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.