title: Can't Group By an internal alias?
body: <p>i have a table called <strong>information_livraison</strong> </p>
  
  <pre><code>Num_bloc    date_livraison
  14104       2012-03-22 09:18:43.000
  14202       2012-03-22 09:18:43.000
  13276       2012-03-22 09:19:39.000
  12775       2012-03-22 09:24:59.000
  12967       2012-03-22 09:29:18.000
  13809       2012-03-22 09:50:14.000
  14611       2012-03-22 10:04:56.000
  10320       2012-03-22 10:05:33.000
  14593       2012-03-22 10:06:20.000
  15179       2012-03-22 10:06:43.000
  </code></pre>
  
  <p>i have to build  a query that select a range of dates and return counts of every date in it is available</p>
  
  <p>my query :</p>
  
  <pre><code>Declare @Days Table (DateField datetime)
  
  Declare @CurrentDate datetime
  Declare @EndDate datetime
  
  Set @CurrentDate = (SELECT DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0))
  Set @EndDate = (SELECT DATEADD(ms,- 3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE())+1,0))))
  
  
  While @CurrentDate &lt;= @EndDate
    Begin
       Insert Into @Days Values(@CurrentDate)
       Set @CurrentDate = DateAdd(d,1,@CurrentDate)
    End 
  
  
  Select convert(varchar(10),DateField,103) as DT ,IsNull(Livre,0) AS Livre
  
  From @Days
  
       LEFT OUTER JOIN 
        (SELECT DATEADD(day, DATEDIFF(day, '20040101', [date_livraison]), '20040101') , Count(*) AS Livre
          FROM Colis.dbo.information_livraison 
          GROUP BY DATEADD(day, DATEDIFF(day, '20040101', [date_livraison]), '20040101')) LV 
        ON DateField = DATEADD(day, DATEDIFF(day, '20040101', [date_livraison]), '20040101')
  
  
   ORDER BY DateField ASC
  
   GO 
  </code></pre>
  
  <p>an error was generated</p>
  
  <blockquote>
    <p>Msg 8155, Level 16, State 2, Line 17<br>
    Aucune colonne spécifiée pour la colonne 1 de 'LV'.<br>
    Msg 207, Level 16, State 1, Line 25<br>
    Nom de colonne non valide : 'date_livraison'.  </p>
  </blockquote>
  
  <p>i want this result</p>
  
  <pre><code>DT          Livre   
  01/03/2012  0
  02/03/2012  0
  03/03/2012  0
  04/03/2012  0
  05/03/2012  0
  ......
  21/03/2012  10
  22/03/2012  0
  ......
  29/03/2012  0
  30/03/2012  0
  31/03/2012  0
  </code></pre>