title: Nesting JSON query result in SQL server 2016
body: <p>I have the following test data:</p>
  
  <pre><code>create table #T (MeterID int,[Description] varchar(255),[Status] varchar(55),Date datetime2(0),Reading int)
  insert into #T select 4201544,'Home test','Active','2016/03/23 06:13',105678
  insert into #T select 4201544,'Home test','Active','2016/03/23 06:14',105682
  insert into #T select 4201544,'Home test','Active','2016/03/23 06:15',105684
  insert into #T select 4201544,'Home test','Active','2016/03/23 06:16',105685
  insert into #T select 4201544,'Home test','Active','2016/03/23 06:17',105686
  insert into #T select 4201544,'Home test','Active','2016/03/23 06:18',105688
  insert into #T select 4201544,'Home test','Active','2016/03/23 06:19',105688
  insert into #T select 4201544,'Home test','Active','2016/03/23 06:20',105689
  insert into #T select 4201544,'Home test','Active','2016/03/23 06:21',105690
  </code></pre>
  
  <p>which I extracted into the desired JSON as follows:</p>
  
  <pre><code>select
      [MeterID],
      [Description],
      [Status],
      (select
          [Date],
          [Reading]
      from
          #T
      for JSON path) [Readings]
  from
      #T
  group by
      MeterID,
      [Description],
      [Status]
  for JSON path, ROOT ('Data')
  </code></pre>
  
  <p>In this case there are very few rows and the query executes fast.  However, in production this will be applied over many more rows.  It seems inefficient to query #T twice.  </p>
  
  <p>Is there a better way to achieve the same result?  I tried unsuccessfully to apply aliases on a single query of #T.</p>