Solved

SQL Pivot Result

Posted on 2014-01-22
10
338 Views
Last Modified: 2014-01-22
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'
,'MonthNo+MonthNames'
,'sum(PaidUnPaid)[]'
,'StudentId,GRNNo,ParentId,StudentName,FatherName,Class,Section,Shift,RollNo'
,''
,'Class,Section,Shift,RollNo'

Open in new window




USE [RHGS2]
GO
/****** Object:  StoredProcedure [dbo].[CrossTabMake]    Script Date: 01/22/2014 22:05:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
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) 
AS 

--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 <> '') 
begin 
set @OtherFields = ', ' + @OtherFields 
end 

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

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 
from 
#Temp 
order by 
thisPivot 

drop table #Temp 

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

set nocount off 
set ansi_warnings on

Open in new window

0
Comment
Question by:Mehram
  • 6
  • 4
10 Comments
 

Author Comment

by:Mehram
ID: 39800521
result
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 39800609
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'.
0
 

Author Comment

by:Mehram
ID: 39800627
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'
,'MonthNo+MonthNames'
,'Sum(PaidUnPaid)[]'
,'StudentId,GRNNo,ParentId,StudentName,FatherName,Class,Section,Shift,RollNo'
,''
,'Class,Section,Shift,RollNo'

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 39800646
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.
0
 

Author Comment

by:Mehram
ID: 39800655
I have change SQL

Please to see screen with error
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Mehram
ID: 39800656
error
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 39800693
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.
0
 

Author Comment

by:Mehram
ID: 39800700
Sir can you change it my query
0
 

Author Comment

by:Mehram
ID: 39800709
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'
,'MonthNo+MonthNames'
,'Max(PaidUnPaid)[]'
,'StudentId,GRNNo,ParentId,StudentName,FatherName,Class,Section,Shift,RollNo'
,''
,'Class,Section,Shift,RollNo'

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 39800716
I am glad you got it and that I could help.

Best regards and happy coding,

Kevin
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now