Solved

Dynamic Pivot Columns

Posted on 2010-08-19
5
266 Views
Last Modified: 2012-05-10
The attached code works...but I need to make the pivot part dynamic because the months will change in my Stored Procedure based on paramaters passed in for BegDate and EndDate.

This is the DYNAMIC I've been trying to get working...no go so far
It returns this error
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

on this sql
declare @sql varchar(4000)
declare @columns varchar(8000)  
set @columns = (select Mth from #temp group by Mth)
 
set @sql = 'SELECT * FROM  
    (SELECT [rep], [GrossRev], [Mth] as Mth from #temp) src
     PIVOT
    (Sum ([GrossRev]) FOR Mth IN (' + @columns + ') ) AS pvt'
 
EXEC (@sql)
SELECT 	v.rep,
		Sum(v.[Gross Rev]) as GrossRev, 
		DATENAME(month, v.Funded) as Mth
Into #temp
FROM	CRMPROD_01.dbo.v_P_Reporting v
WHERE	v.Funded is not null 
		and	v.Status <> 'Closed' 
		and (YEAR(v.Funded) = YEAR(getdate())) 
		and	(MONTH(v.Funded) > MONTH(getdate())-3)
Group by rep, v.Funded
ORDER BY v.rep, DATENAME(month, v.Funded)

SELECT Rep,June,July  
FROM (  
SELECT Rep,Mth,Grossrev FROM #temp)P  
PIVOT   
(SUM(Grossrev) FOR Mth IN (June,July))As Pvt
order by rep

Open in new window

0
Comment
Question by:lrbrister
  • 3
  • 2
5 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33480461
declare @sql varchar(4000)
declare @columns varchar(8000)  

select @columns = coalesce(@columns+',','') + '[' + Mth + ']'
from #temp
group by Mth
 
set @sql = '
    SELECT [rep], ' + @columns + '
    FROM  
    (SELECT [rep], [GrossRev], [Mth] as Mth from #temp) src
     PIVOT
    (Sum ([GrossRev]) FOR Mth IN (' + @columns + ') ) AS pvt'
 
EXEC (@sql)
0
 

Author Comment

by:lrbrister
ID: 33483880
cyberkiwi:
That's almost perfect...but it doesn't put the month in the correct ascending order.
In my attached code the resulting table is
 
rep          April       February       January    etc......
myrep     1000      246                    1458    etc...

SELECT  v.rep,  
                Sum(v.[Gross Rev]) as GrossRev,   
                DATENAME(month, v.Funded) as Mth  
Into #temp  
FROM    CRMPROD_01.dbo.v_P_Reporting v  
WHERE   v.Funded is not null   
                and     v.Status <> 'Closed'   
                and (YEAR(v.Funded) = YEAR(getdate()))   
                and     (MONTH(v.Funded) > MONTH(getdate())-8)  
Group by rep, v.Funded  
ORDER BY v.rep, DATENAME(month, v.Funded)  


--Dynamic SQl and Pivot 
declare @sql varchar(4000)
declare @columns varchar(8000)  

select @columns = coalesce(@columns+',','') + '[' + Mth + ']'
from #temp
group by Mth
 
set @sql = '
    SELECT [rep], ' + @columns + '
    FROM  
    (SELECT [rep], [GrossRev], [Mth] as Mth from #temp) src
     PIVOT
    (Sum ([GrossRev]) FOR Mth IN (' + @columns + ') ) AS pvt'
 
EXEC (@sql)

drop table #temp

Open in new window

0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33483974
I suspect just changing the order by in the insert clause will work.  It is currently going in alphabetically.
But just to be safe...
SELECT  v.rep,  
                Sum(v.[Gross Rev]) as GrossRev,   
                DATENAME(month, v.Funded) as Mth  ,
                month(v.Funded) as MthI  
Into #temp  
FROM    CRMPROD_01.dbo.v_P_Reporting v  
WHERE   v.Funded is not null   
                and     v.Status <> 'Closed'   
                and (YEAR(v.Funded) = YEAR(getdate()))   
                and     (MONTH(v.Funded) > MONTH(getdate())-8)  
Group by rep, v.Funded  
ORDER BY v.rep, month(v.Funded)

--Dynamic SQl and Pivot 
declare @sql varchar(4000)
declare @columns varchar(8000)  

select @columns = coalesce(@columns+',','') + '[' + Mth + ']'
from #temp
group by Mth, MthI
order by MthI
 
set @sql = '
    SELECT [rep], ' + @columns + '
    FROM  
    (SELECT [rep], [GrossRev], [Mth] as Mth from #temp) src
     PIVOT
    (Sum ([GrossRev]) FOR Mth IN (' + @columns + ') ) AS pvt'
 
EXEC (@sql)

drop table #temp

Open in new window

0
 

Author Comment

by:lrbrister
ID: 33484018
cyberkiwi:
Perfect!  Remind me to buy you a sody-pop sometime!
0
 

Author Closing Comment

by:lrbrister
ID: 33484169
Perfiect!  Please look for follow-up question
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

816 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

8 Experts available now in Live!

Get 1:1 Help Now