title: How to insert Select Query Result into a PIVOT
body: <p>I m using SQL SERVER 2012.</p>
  
  <p>Query:1</p>
  
  <pre><code>SELECT *
  FROM (
      SELECT Insurance, ChargeValue, CreatedDate
      FROM dailychargesummary
      WHERE MonthName='June 2017'
  ) m
  PIVOT (
      SUM(ChargeValue)
      FOR CreatedDate IN ([06/22/2017], [06/23/2017],[06/30/2017])
  ) n
  </code></pre>
  
  <p>Output of above query is looks like below:</p>
  
  <p><a href="https://i.stack.imgur.com/vsF7J.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vsF7J.png" alt="enter image description here"></a></p>
  
  <p>Now I m hard coding all the dates of a month inside the Pivot Query such as 06/01/2017, 06/02/2017, etc., After searching in the Google, I got the following query to display all the dates of a given month number.</p>
  
  <p>Query 2:</p>
  
  <pre><code>DECLARE @month AS INT = 5
  DECLARE @Year AS INT = 2016
  
  ;WITH N(N)AS 
  (SELECT 1 FROM(VALUES(1),(1),(1),(1),(1),(1))M(N)),
  tally(N)AS(SELECT ROW_NUMBER()OVER(ORDER BY N.N)FROM N,N a)
  SELECT datefromparts(@year,@month,N) date FROM tally
  WHERE N &lt;= day(EOMONTH(datefromparts(@year,@month,1)))
  </code></pre>
  
  <p>Output looks like below:</p>
  
  <p><a href="https://i.stack.imgur.com/6OX7q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6OX7q.png" alt="enter image description here"></a></p>
  
  <p>Can anyone please guide me how to use the Query2 inside the pivot in Query1 to automate the dates.</p>