?
Solved

SQL Pivot Result Into A Table

Posted on 2014-02-10
2
Medium Priority
?
392 Views
Last Modified: 2014-02-10
I have a SP for making crosstab report. Which is displaying result perfectly. I only need result into a table XXX to use it further.

Execute Command
Begin
Exec CrossTabMake 'SELECT Transno,ExamCode,Cyear,PC,fn,Seckion,shift,StudentName,Grn, Roll_No,SN,ObtMarks FROM TempResult'
,'SN'
,'Max(ObtMarks)[]'
,'Transno,ExamCode,Cyear,PC,fn,Seckion,shift,StudentName,Grn, Roll_No'
,''
,'PC,Seckion,Shift,Roll_No'
End

Open in new window


Store procedure - SP
GO
/****** Object:  StoredProcedure [dbo].[CrossTabMake]    Script Date: 02/11/2014 11:10:27 ******/
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 

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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 1500 total points
ID: 39849311
did you mean to say you need to insert tne result in third table something like this


Insert into table_XXX
 
exec( 'select ' + @GroupBy + @OtherFields + @Vals + @Vals2 + @Vals3 +
' from (' + @SQL + ') A GROUP BY ' + @GroupBy + @roll + @OrderByFields)
0
 

Author Comment

by:Mehram
ID: 39849312
yes,
Can you change my code accordingly.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

752 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