title: how do i create a (temporary?) stored procedure to assist making a summary view?
body: <p>I have view table that looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/BRFMH.png" alt="enter image description here"></p>
  
  <p>And I started to create a view to summarize by status for each currency:</p>
  
  <pre><code>WITH C AS (SELECT * FROM CampaignsPublisherReportSummary)
  SELECT
      'Total' T,
      SUM(CASE WHEN PayCurrency='USD' THEN Total END) USD,
      SUM(CASE WHEN PayCurrency='EUR' THEN Total END) EUR,
      SUM(CASE WHEN PayCurrency='GBP' THEN Total END) GBP,
      SUM(CASE WHEN PayCurrency='AUD' THEN Total END) AUD
  FROM C
  UNION
  SELECT
      'ToBePaid' T,
      SUM(CASE WHEN PayCurrency='USD' THEN ToBePaid END) USD,
      SUM(CASE WHEN PayCurrency='EUR' THEN ToBePaid END) EUR,
      SUM(CASE WHEN PayCurrency='GBP' THEN ToBePaid END) GBP,
      SUM(CASE WHEN PayCurrency='AUD' THEN ToBePaid END) AUD
  FROM C
  GO
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/4gEXK.png" alt="enter image description here"></p>
  
  <p>I could finish and get what I want by copy/pasting again for each of the other status (Unverified, Verified, ...etc) but the programmer in me wants to have some kind of function that takes the the status name and runs a templatized version of the query...</p>
  
  <p>QUESTION: Can I do this?  Or is there another better way to break the information down that I'm missing?</p>
  
  <p>UPDATE: My goal is to have something like this:</p>
  
  <pre><code>SELECT * FROM #mytempfunc('Total')
  UNION SELECT * FROM #mytempfunc('ToBePaid')
  UNION ...
  </code></pre>