Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Use returned data inside procedure
Message
De
22/09/2015 01:34:56
 
 
À
21/09/2015 21:10:07
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2014
Application:
Web
Divers
Thread ID:
01624886
Message ID:
01624890
Vues:
45
Hi, John,

I'm shutting down for the night....but if I understand your question.....here's how you could do it. I'm doing this based on the assumption that having a final row at the end of the table for a "total row" would be sufficient. I've attached a screen shot of what my code below will generate.

If not, I'll try to help tomorrow.

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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform