Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Use returned data inside procedure
Message
From
22/09/2015 08:50:23
 
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2014
Application:
Web
Miscellaneous
Thread ID:
01624886
Message ID:
01624907
Views:
44
>>But if a total summarized row at the end is what you're looking for....again, because you're generating the columns dynamically, you want think in terms of, "how would I like for the generated query to run at the end?' Well, the generated query could use a UNION ALL to requery from the pivoted results. So the generated query could look like this:
;WITH PIVOTCTE as 
 (select QuoteDate as ActivityDate, Client, 'Quotes' as ActionType,  NumQuotes as Qty  FROM dbo.QuoteMaster
UNION
 SELECT CancelDate as ActivityDate, Client, Problem as ActionType,  Qty from dbo.CancellationMaster) ,  

 FinalPivotCTE as  (SELECT     ActivityDate, Client, [Quotes],[Declined at begining],[No Credit Card],[Not Ended] 
     FROM PIVOTCTE 
            pivot ( SUM(Qty) for ActionType in ([Quotes],[Declined at begining],[No Credit Card],[Not Ended])) TEMPLIST ) 


select * from FinalPivotCTE UNION ALL
   SELECT NULL as ActivityDate, NULL AS Client, SUM([Quotes]),SUM([Declined at begining]),SUM([No Credit Card]),SUM([Not Ended])
   FROM FinalPivotCTE
OK, now for modifying the original procedure to add to the generated code, to SUM all the dynamic columns, here's a new procedure that does everything the first one did. Note that in the code I've added an additional string that figures out what columns to SUM...
CREATE PROCEDURE dbo.DynamicPivotExampleWithTotals
as
begin

 DECLARE @ActionSelectString nvarchar(4000) , @SQLPivotQuery nvarchar(4000)
 
 ;WITH ListTempCTE as  
    (SELECT 'Quotes' AS ActionType , 1 as OrderNum
			                   UNION
							      SELECT DISTINCT Problem as Actiontype, 2 as OrderNum FROM CancellationMaster )
  							 
 SELECT @ActionSelectString =  stuff (  (  select ',[' +  
           cast(ActionType as varchar(100))  + 
		    ']'                from  ListTempCTE
                      	ORDER BY OrderNum, ActionType
						  for xml path('') ), 1, 1, '')

DECLARE @SumString nvarchar(4000)
SET @SumString = REPLACE( @ActionSelectString, '[', 'SUM([')
SET @SumString = REPLACE( @SumString, ']', '])' )
 
SET @SQLPivotQuery = ';WITH PIVOTCTE as 
 (select QuoteDate as ActivityDate, Client, ''Quotes'' as ActionType,  NumQuotes as Qty  FROM dbo.QuoteMaster
UNION
 SELECT CancelDate as ActivityDate, Client, Problem as ActionType,  Qty from dbo.CancellationMaster) ,  '

 set @SQLPivotQuery = @SqlPivotQuery + 
       ' FinalPivotCTE as  (SELECT     ActivityDate, Client, ' + @ActionSelectString +
     ' FROM PIVOTCTE 
            pivot ( SUM(Qty) for ActionType in ('
            +  @ActionSelectString + ')) TEMPLIST ) ' + char(13)  + char(10)

set @SQLPivotQuery = @SQLPivotQuery + 
'select * from FinalPivotCTE UNION ALL
   SELECT NULL as ActivityDate, NULL AS Client, ' + @SumString + ' FROM FinalPivotCTE'
    
			 
 EXEC sp_executesql @SqlPivotQuery

end
Wonderful Kevin, thank you very much, this will help me to improve the code you gave me before. Sorry I didn't reply yesterday I got disconnected before you. Right now what I'm dealing with is this:
I was asked to return 2 grids, first one grouped by days in the number of days end user specifies and the second grid the whole data from date start to date end grouped by week. In example, the end user wants quotes from 09/01/2015 to 09/22/2015, first grid with the last 10 days (He wants that parameter). So what the page must display is a first grid grouped by day and showing data from 09/13/2015 to 09/22/2015. Then display a second grid grouped by week and showing data from 09/01/2015 to 09/22/2015. What do you think?
John Harold Belalcázar Lozano
Associate Director Of Development
http://www.belvicto.co/
jhbelalc@gmail.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform