title: getting "No column was specified for column 2 of 'd'" in sql server cte?
body: <p>I have this query, but its not working as it should,</p>
  
  <pre><code>with c as (select 
                 month(bookingdate) as duration, 
                 count(*) as totalbookings 
             from 
                 entbookings
             group by month(bookingdate)
            ),
       d as (SELECT 
                 duration, 
                 sum(totalitems) 
             FROM 
                 [DrySoftBranch].[dbo].[mnthItemWiseTotalQty] ('1') AS BkdQty
             group by duration
            )
  
  select 
      c.duration, 
      c.totalbookings, 
      d.bkdqty 
  from
      c 
      inner join d 
      on c.duration = d.duration
  </code></pre>
  
  <p>when I run this, I am getting </p>
  
  <blockquote>
    <p><em>Msg 8155, Level 16, State 2, Line 1<br>
    No column was specified for column 2 of 'd'.</em></p>
  </blockquote>
  
  <p>Can any one tell me what am I doing wrong?</p>
  
  <p>Also, when I run this, </p>
  
  <pre><code>with c as (select 
                 month(bookingdate) as duration, 
                 count(*) as totalbookings 
             from 
                 entbookings
             group by month(bookingdate)
            ),
       d as (select 
                 month(clothdeliverydate), 
                 SUM(CONVERT(INT, deliveredqty)) 
             FROM 
                 barcodetable
             where 
                 month(clothdeliverydate) is not null
                 group by month(clothdeliverydate)
            )
  
  select 
      c.duration, 
      c.totalbookings, 
      d.bkdqty 
  from
      c 
      inner join d 
      on c.duration = d.duration
  </code></pre>
  
  <p>I get </p>
  
  <blockquote>
    <p><em>Msg 8155, Level 16, State 2, Line 1<br>
    No column was specified for column 1 of 'd'.<br>
    Msg 8155, Level 16, State 2, Line 1<br>
    No column was specified for column 2 of 'd'.</em></p>
  </blockquote>